OpenCV copyTo assert error - c++

While copying one Mat into the region of interest of another I came accross an error I've never seen before. Googling it didn't turn up many results and none of them seems to be relevant.
I have included a screenshot of the error as well as some properties of the Mat's.
This is the code:
std::cout << "size height,width: " << size.height << ", " << size.width << std::endl;
cv::Mat tempResult(size.width, size.height, result.type());
std::cout << "tempResult cols,rows: " << tempResult.cols << ", " << tempResult.rows << std::endl;
std::cout << "tempResult type: " << tempResult.type() << std::endl;
std::cout << "tempResult channels: " << tempResult.channels() << std::endl;
std::cout << "result cols,rows: " << result.cols << ", " << result.rows << std::endl;
std::cout << "result type: " << result.type() << std::endl;
std::cout << "result channels: " << result.channels() << std::endl;
cv::Rect rect(0, 0, result.cols-1, result.rows-1);
std::cout << "rect size: " << rect.size() << std::endl;
result.copyTo(tempResult(rect));

The cv::Mat::operator(cv::Rect roi) method extract a submatrix with the same size of the cv::Rect roi.
But you defined a cv::Rect object with 1 row and 1 col missing, so the output matrix returned by tempResult(rect) is smaller the the matrix result. cv::Mat::CopyTo launch an exception because the input to copy is smaller than the output argument.
To fix this :
cv::Rect rect(0, 0, result.cols, result.rows);

For cv::Rect, its format is (x, y, width, height), not (x1, y1, x2, y2). That's why, in my opinion, you get the error.
If yes, you will need to change rect to:
cv::Rect rect(0, 0, result.cols, result.rows);
If not (i.e. you really means rect(x, y, width-1, height-1)), you can do like this:
result(rect).copyTo(tempResult(rect));

Related

Why Ogre::Matrix4::extract3x3Matrix and Ogre::Matrix4::decomposition doesn't return the same rotation?

I have a Ogre::Matrix4, and I want to get the quaternion of this rotation.
For this I use the extract3x3Matrix function then I am transforming this matrix in quaternion which give me one response. Then I am using the decompose function of Ogre::Matrix4 which give me an other quaternion.
The 2 quaternions are different.
How is it possible ?
Before applying the decompose function, I'm making sure the transformation is affine.
Ogre::Matrix4 car_T_tracker( 0.0124343, -0.04752782, 1.010372, -1400.69,
0.3480031, -0.9485609, -0.04890297, 647.4758,
-0.9497393, -0.3481936, -0.004690885, 1441.177,
0, 0, 0, 1);
Ogre::Matrix3 rotation_car_T_tracker;
car_T_tracker.extract3x3Matrix(rotation_car_T_tracker);
Ogre::Quaternion q;
q = car_T_tracker.extractQuaternion();
std::cout << "quaternion x : " << q.x << std::endl;
std::cout << "quaternion y : " << q.y << std::endl;
std::cout << "quaternion z : " << q.z << std::endl;
std::cout << "quaternion w : " << q.w << std::endl;
Ogre::Vector3 translation_car_T_tracker;
Ogre::Vector3 scale_car_T_tracker;
Ogre::Quaternion quaternion_car_T_tracker;
if(car_T_tracker.isAffine())
{
car_T_tracker.decomposition (translation_car_T_tracker,scale_car_T_tracker,quaternion_car_T_tracker);
}
else
{
translation_car_T_tracker = Ogre::Vector3::ZERO;
scale_car_T_tracker = Ogre::Vector3::ZERO;
quaternion_car_T_tracker = Ogre::Quaternion::ZERO;
}
std::cout << "translation : " << translation_car_T_tracker << std::endl;
std::cout << "scale : " << scale_car_T_tracker << std::endl;
std::cout << "quaternion : " << quaternion_car_T_tracker << std::endl;
quaternion_car_T_tracker and q are not the same quaternion but they should be.

Accessing and using data returned by SDL_QueryTexture() causes segfault

I'm trying to simply ascertain the width and height of a given texture. SDL_QueryTexture() seems to return with no errors, but the moment I try at actually access and use the data, it segfaults.
When stepping through with the debugger this line causes the fault:
cout << "width is : " << *w << " height is : " << *h << endl;
Am I missing something here? Maybe I'm misinterpreting the documentation?
I'm using Netbeans on Windows with MinGW's compiler.
testCharacter = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 3, 5);
int* w;
int* h;
Uint32* format;
int* access;
if (SDL_QueryTexture(testCharacter, format, access, w, h) != 0) {
cout << "ERROR : " << SDL_GetError() << endl;
} else {
cout << "no error" << endl;
}
cout << "width is : " << *w << " height is : " << *h << endl;
Also the above code is the exact code that produces the error.

Issue with imread. Strange float values

I'm trying to simple load an image (TIFF) and display a pixel value.
If I open the image using ImageJ the values are 32-bit float. But opening the same image using opencv I get really strange float values, e.g. 4.2039e-44.
If I read the value of a specific pixel using "int" the presented value is correct. Below is the code I use to test. And here a link for the image: https://goo.gl/Wmv9xE.
Thanks in advance.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
int main(int argc, char **argv) {
std::string imageFile = "image.tiff";
cv::Mat image;
image = cv::imread(imageFile, CV_LOAD_IMAGE_ANYDEPTH); // Read the file
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the image: " << imageFile << std::endl;
return -1;
}
std::cout << "Image:" << image.rows << " x " << image.cols << " Channels: " << image.channels() << " Depth: " << image.depth() << std::endl;
std::cout << "Value at 0,0: " << image.at<float>(0,1)<< std::endl; // Strange Value
std::cout << "Value at 0,0: " << image.at<int>(0,1)<< std::endl; // Correct Value
return (EXIT_SUCCESS);
}
* Update *
Trying to move forward with the code, I decided to create a function to convert the data to "int" reading from the file.
As a temporary solution this worked, but I'm still looking for the reason why the data is being loaded wrong.
int main(int argc, char **argv) {
std::string imageFile = "/home/slepicka/XSConfig/image.tiff";
cv::Mat image = openImage(imageFile);
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the image: " << imageFile << std::endl;
return -1;
}
std::cout << "Image:" << image.rows << " x " << image.cols << " Channels: " << image.channels() << " Depth: " << image.depth() << " Type: " << image.type() << std::endl;
std::cout << "Value at 0,0: " << image.at<int>(0,1)<< std::endl; // Correct Value
//std::cout << "Data: " << image << std::endl;
return (EXIT_SUCCESS);
}
cv::Mat openImage(std::string filename){
cv::Mat imageLoad = cv::imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
if(imageLoad.type() == CV_32F){
return convertToInt(imageLoad);
}
return imageLoad;
}
cv::Mat convertToInt(cv::Mat source){
int r, c;
cv::Mat converted;
converted.create(source.rows, source.cols, CV_32SC1);
for (r=0; r<source.rows;r++) {
for (c=0; c<source.cols;c++) {
converted.at<int>(r, c) = source.at<int>(r, c);
}
}
return converted;
}

Error in HOG descriptor

I am using HOG descriptor for feature extraction. I am using visual studio 2012 and opencv 2.4.9 version. I am getting run time error in hog.compute function.
int main()
{
Mat img_raw = imread("p1.jpg", 1); // load as color image.
Mat img;
cvtColor(img_raw, img, CV_RGB2GRAY);
HOGDescriptor hog;
vector<float> descriptor;
vector<Point>locations;
hog.compute(img, descriptor,Size(32,32),Size(0,0),locations);
cout << "HOG descriptor size is " << hog.getDescriptorSize() << endl;
cout << "img dimensions: " << img.cols << " width x " << img.rows << "height" << endl;
cout << "Found " << descriptor.size() << " descriptor values" << endl;
cout << "Nr of locations specified : " << locations.size() << endl;
return 0;
}

reshaping a matrix failed in OpenCV 2.4.3

I am using OpenCV 2.4.3 to create and reshape a matrix like this:
cv::Mat testMat = cv::Mat::zeros ( 500, 200, CV_8UC3 );
std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
testMat.reshape ( 0, 1 );
std::cout << " size of reshaped testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
Then from the output, I see there is no change for the reshaped testMat. I used "reshape" many times in older version of OpenCV, but with this new version, I couldn't see any changes. Is this a bug? Or am I using it incorrectly here?
reshape returns a new Mat header
cv::Mat testMat = cv::Mat::zeros ( 500, 200, CV_8UC3 );
std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
cv::Mat result = testMat.reshape ( 0, 1 );
std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;