How to get access to vector mat object in OpenCV - c++

I would like to load some pictures from files to Mat-objects (OpenCV) and want to store them in a vector. Further OpenCV calls/objects need vectors (like: AlignMTB) as arguments. But after filling the vector with Mat-objects I'm only able to get access to the last element I added to the vector.
In the example I first load the images to an intermediary Mat-object and convert it to CV_32FC3. Then I print out the BGR-values of one sample-pixel. The print out is:
File 0: 13 13 157
File 1: 17 20 159
File 2: 8 8 152
Then I add this intermediary Mat to the Mat-vector images.
Afterwards I'm trying to print out the sample-pixel values of the first and second image but always I get the values of the third image:
File 0: 8 8 152
File 1: 8 8 152
What I'm making wrong during accessing the vector data?
I was trying it with this routine:
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img);
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;

The problem is not vector::push_back, because it will construct a copy of the given element. However, the problem is the copy constructor of Mat which does not copy the associated data:
No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it.
What you can do to solve the problem is either an explicit Mat::clone operation which also copies the data or to move the matrix declaration in the for loop.
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img.clone());
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;

Related

Is it possible to get the valid hex address of CHAR in C++?

I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);

Why isn't half of my cout statement printing?

I'm printing a statement from classes using getter functions. The top 1.5 lines of my cout statement are not printing. I have tried flushing the stream, and I also copied and pasted the lines that are not printing outside the if statement, and it prints! I can't figure out what is going on. Here is the function:
// display all books out on loan
void displayBorrowed(vector<LibraryBook>& book)
{
cout << "Books currently checked out: " << endl << endl;
for(unsigned int i = 0; i < book.size(); i++)
{
//cout << "ID: " << book[i].getId_() << " Title: "
//<< book[i].getTitle_() << endl << endl;
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << book[i].getId_() << " Title: "
<< book[i].getTitle_() << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
}
}
It displays this:
Books currently checked out:
Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
If the lines in the if statement are copied out of the if statement it displays normally:
ID: 78620 Title: Zhuan Falun
I tried changing the if statement to false to display all the books not loaned, and they all displayed the same except for the very last book (number 50 finally displayed the id # and the title. I am at a complete loss. What is going on?
It should look like this:
ID: 78620 Title: Zhuan Falun Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
(havent formatted display yet)
I just changed it to this where I have every single element that doesn't display in its own cout statement, and NONE of it displays!! What??! (up until author, where it started displaying before, I mean.)
for(unsigned int i = 0; i < book.size(); i++)
{
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " ;
cout << book[i].getId_();
cout << " Title: ";
cout << book[i].getTitle_();
cout << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
It prints when I put an endl at the end of each element:
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << endl;
cout << book[i].getId_() << endl;
cout << " Title: " << endl;
cout << book[i].getTitle_() << endl;
cout << " Author: " << endl;
cout << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_() << endl;
cout << " Year Published: " << book[i].getYearPubl_() << endl;
cout << "Due Date: " << book[i].getDueMonth_() << "/" << endl;
cout << book[i].getDueDay_() << "/" << book[i].getDueYear_() << endl;
cout << " Date Borrowed: " << book[i].getBorrwdMonth_() << "/" << endl;
cout << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_() << endl;
cout << endl << "Checked out by: " << book[i].getBorrwFirst_() << endl;
cout << " " << book[i].getBorrwLast_() << endl << endl;
}
Books currently checked out:
ID:
47492
Title:
Borstal Boy
Author:
Brendan Behan
Year Published: 1958
Due Date: 8/
2/2017
Date Borrowed: 7/
21/2017
Checked out by: Cassie
Peterson
My suggestion:
Print each of the members in their own line.
Find out which member is problematic.
Dig deeper into the contents of the member to understand how it got there and fix it.
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
std::cout << "ID: " << book[i].getId_() << std::endl
<< "Title: " << book[i].getTitle_() << std::endl
<< "Author First: " << book[i].getAuthorFirst_() << std::endl
<< "Author Last:" << book[i].getAuthorLast_() << std::endl
<< "Year Published: " << book[i].getYearPubl_() << std::endl
<< "Due Date Month: " << book[i].getDueMonth_() << std::endl
<< "Due Date Day: " << book[i].getDueDay_() << std::endl
<< "Due Date Year: " << book[i].getDueYear_() << std::endl
<< "Borrowed Month: " << book[i].getBorrwdMonth_() << std::endl
<< "Borrowed Day: " << book[i].getBorrwdDay_() << std::endl
<< "Borrowed Year: " book[i].getBorrwdYear_() << std::endl
<< "Checked out by first: " << book[i].getBorrwFirst_() << std::endl
<< "Checked out by last: " << book[i].getBorrwLast_() << std::endl
<< std::endl;
}
}
/* Sometimes "half cout statement not printing problem" may occur due to use of dangling or dangerous pointers. In the code below all cout statements that present, after dangling pointer printing statement are not printing anything.
If we remove the dangling pointer's cout statement then everything will be fine.
code:
// dangling pointer is dangerous because it can hold undesired address. */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a =5;
char b= 'a';
int *p = &a;
char *p1 =&b;
int *p3; // p3 is dangling pointer
cout<<"size of p "<<sizeof(p)<<endl; // o/p is 8, bcoz x64 bit compiler
gives 8byte and x32 gives 4 byte.
cout<<"size of p1 "<<sizeof(p1)<<endl;
cout<<"size of p3 "<<sizeof(p3)<<endl;
cout<<"address of p3 is "<<&p3<<endl;
cout<< "value at p3 is "<<*p3<<endl; //***dangling or dangerous pointer.***
cout<<"hello my name is rahul ";
cout<<a<<endl;
cout<<&a<<endl;
//cout<<*a<<endl; // shows error (invalid type argument)
cout<<p<<endl; // shows base address
cout<<&p<<endl; // shows pointers address
cout<<*p<<endl;
cout<<"this is an end"<<endl;
return 0;
}

Weird hexadecimal answers to simple math in code::blocks

I'm trying to do the following assignment in a c++ book.
After running this:
#include <iostream>
using namespace std;
int main()
{
double first_arg;
double second_arg;
cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
cout << first_arg << " + " << second_arg << " = "
<< cout << first_arg + second_arg << "\n";
cout << first_arg << " / " << second_arg << " = "
<< cout << first_arg / second_arg << "\n";
cout << first_arg << " - " << second_arg << " = "
<< cout << first_arg - second_arg << "\n";
I get some unexpected results. Like this result copied straight from the windows cli:
Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2
I'm using the latest version of codeblocks with the default compiler settings. Thanks.
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
You have two cout in one line since there is no semicolon on line 1
To fix this either get rid of the second cout or add a semicolon at the end of the first line on each cout statement.
If you look at the last 2 digits of each answer you will see the answer you wish to get so its still printing out the answer you want its just after the pointer to cout.

How to make an array print zeros when there is no input in that slot

The user inputs a double and string which gets stored into two arrays. Like so :
{
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
Then the double and string are passed to this :
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
last10charges[i] = last10charges[i-1];
}
last10withdraws[0] = amount;
last10charges[0] = name;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :
{
cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
cout << " " << last10withdraws[0] << " " << last10deposits[0] << " " << last10charges[0] << endl;
cout << " " << last10withdraws[1] << " " << last10deposits[1] << " " << last10charges[1] << endl;
cout << " " << last10withdraws[2] << " " << last10deposits[2] << " " << last10charges[2] << endl;
cout << " " << last10withdraws[3] << " " << last10deposits[3] << " " << last10charges[3] << endl;
cout << " " << last10withdraws[4] << " " << last10deposits[4] << " " << last10charges[4] << endl;
cout << " " << last10withdraws[5] << " " << last10deposits[5] << " " << last10charges[5] << endl;
cout << " " << last10withdraws[6] << " " << last10deposits[6] << " " << last10charges[6] << endl;
cout << " " << last10withdraws[7] << " " << last10deposits[7] << " " << last10charges[7] << endl;
cout << " " << last10withdraws[8] << " " << last10deposits[8] << " " << last10charges[8] << endl;
cout << " " << last10withdraws[9] << " " << last10deposits[9] << " " << last10charges[9] << endl;
cout << endl;
}
Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :
60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?
Initialize the array as:
int last10withdraws[10] = {0};
Now all elements are zero.
If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.
If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
Hope that helps.
You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.

I want to print out some values from 2-d array C++

I want to input data from txt file.
the file contains 2-d array [5][5]
how can i print out the any value i want?
i don't want to print out the whole 5*5 data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double distance[5][5] ;
string line;
ifstream ratefile;
ratefile.open("a.txt");
ofstream file;
if (ratefile.is_open())
{
while (! ratefile.eof() )
{
getline (ratefile,line);
ratefile.getline(distance, 25, '*');
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
cin.get();
return 0;
}
If you only want to output one value and the user should be able to choose a value, you can do something like this:
int x, y;
cin >> x;
cin >> y;
cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y];
But you should check if the user enter valid numbers (0 <= x < 4 and 0 <= y < 4)
There is part of the code missing, but you are printing values you want. Simply remove the lines you don't want to print.
Of course you can also use variables:
int x = 2,y = 2;
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y];