Why does opencv not show a CV_32S image? - opencv

I am using OpenCV-4.2.0 (contrib). OpenCV does not show a labeled image created with connectedComponentsWithStats.
The relevant code is:
//Connected Compnents
std::cout << "Calculating Connected Components..." << std::endl;
Mat background_mask_labels(background_mask.size(), CV_32S);
Mat cc_stats,cc_centroids;
int nLabels = cv::connectedComponentsWithStats(background_mask, background_mask_labels, cc_stats, cc_centroids, 4);
// show output
imshow("Background Image", background_mask);
imshow("Background Labels", background_mask_labels);
waitKey(0);
When running the program in x64 Debug mode it throws an error:
OpenCV(4.2.0) Error: Assertion failed (src_depth != CV_16F && src_depth != CV_32S) in convertToShow, file c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\precomp.hpp, line 137
at
imshow("Background Labels", background_mask_labels);
But I dont't understand the error, since I declared background_mask_labels to be CV_32S in first place.
Even a background_mask_labels.convertTo(background_mask_labels , CV_32S); after the connectedComponents does not help - same output.
Thanks for helping!

Related

OpenCV VideoCapture set function does not change frame rate FPS?

Good afternoon everybody,
In OpenCV I'm having trouble getting the VideoCapture's set function to change the frame rate. Here is my test program, using the "768x576.avi" test video file from the "C:\OpenCV-3.1.0\opencv\sources\samples\data" directory
// VideoCaptureSetTest.cpp
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<conio.h> // it may be necessary to change or remove this line if not using Windows
///////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
cv::VideoCapture capVideo;
cv::Mat imgFrame;
capVideo.open("768x576.avi");
if (!capVideo.isOpened()) { // if unable to open video file
std::cout << "error reading video file" << std::endl << std::endl; // show error message
_getch(); // it may be necessary to change or remove this line if not using Windows
return(0); // and exit program
}
char chCheckForEscKey = 0;
double dblFPS = capVideo.get(CV_CAP_PROP_FPS);
std::cout << "1st time - dblFPS = " << dblFPS << "\n"; // this prints "10" to std out as expected
bool blnSetReturnValue = capVideo.set(CV_CAP_PROP_FPS, 5);
std::cout << "2nd time - dblFPS = " << dblFPS << "\n"; // this also prints "10", why not "5" as expected ??!!
std::cout << "blnSetReturnValue = " << blnSetReturnValue << std::endl; // this prints "0" (i.e. false)
while (chCheckForEscKey != 27) {
capVideo.read(imgFrame);
if (imgFrame.empty()) break;
cv::imshow("imgFrame", imgFrame);
chCheckForEscKey = cv::waitKey(1);
}
return(0);
}
I'm using a very standard setup here, OpenCV 3.1.0, Visual Studio 2015, Windows 10, and the .avi file I'm testing with is the one that ships with OpenCV.
No matter what I attempt to set the FPS to, it stays at 10 and the set function always returns false.
Yes, I'm aware of the hack fix of setting the cv::waitKey() parameter to a larger value to achieve a certain delay, but this would then be computer-dependent and I may need to run video on various computers in the future so this is not an option.
Am I doing something wrong? Is the VideoCapture::set function known to not work in some cases? Has anybody else experienced the same results? I checked the OpenCV issue tracker and did not find anything to this effect. Is this a bug I should raise a ticket for? Anybody else with some experience with this please advise.

OpenCV3.0 gold imread function doesn't work with absolute path on Ubuntu14.04

I use imread function by OpenCV3.0 gold on Ubuntu 14.04, and followed the web to installed OpenCV3.0 But the imread function dosen't work with absolute path.It can work like imread("a.jpg"),but not imread("\home\a\a.jpg") .I want to use the function to read image sequence.Here is my code:
char filename1[200];
char filename2[200];
sprintf(filename1, "/home/image_2/%06d.png", 0);
sprintf(filename2, "/home/image_2/%06d.png", 1);
//read the first two frames from the dataset
Mat img_1_c = imread(filename1);
Mat img_2_c = imread(filename2);
if ( !img_1_c.data || !img_2_c.data ) {
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
There are images in folder a,like 000000.png .When I run it ,it says"--(!) Error reading images".Can someone help me ?Thank you.

Assertion Failed when using fisheye undistortPoints

I am trying to undistort a fisheye image taken from a camera. I have already gotten the camera parameters needed. However, when I run the code below:
Mat cammatrix = cv::Mat::zeros(3,3, CV_64F);
cammatrix.at<double>(0,0) = 3.7089418826568277e+002;
cammatrix.at<double>(1,1) = 3.7179355652545451e+002;
cammatrix.at<double>(0,2) = 3.4450520804288089e+002;
cammatrix.at<double>(1,2) = 2.5859133287932718e+002;
cammatrix.at<double>(2,2) = 1.0;
std::vector<double> distortcoeff;
double tempdoub = -2.2022994789941803e+000;
distortcoeff.push_back(tempdoub);
tempdoub = 4.4652133910671958e+000;
distortcoeff.push_back(tempdoub);
tempdoub = 6.8050071879644780e-001;
distortcoeff.push_back(tempdoub);
tempdoub = -1.7697153575434696e+000;
distortcoeff.push_back(tempdoub);
// Process images here (optional)
Mat img_scene (current);
if(!img_scene.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
img_scene.convertTo(img_scene, CV_32FC2);
cv::fisheye::undistortPoints(img_scene, img_scene, cammatrix, distortcoeff);
I get this error:
OpenCV Error: Assertion failed (distorted.type() == CV_32FC2 || distorted.type() == CV_64FC2) in undistortPoints
Not sure why this is happening because I have the .convertTo line right before converting it to CV_32FC2. If anyone could help me fix this error, I would really appreciate it!
undistortPoints() function is retrieving undistorted pixel location, given it's current distorted location on the image. i.e it operates on points, not on images.
use fisheye::undistortImage for images.

MatToQImage QPixmap::scaled: Pixmap is a null pixmap

I Try to display a video processed by OpenCV, which is in Mat format, to a Qt self-defined FrameLabel. Mat frames can be populated when I add the OpenCV function imshow("frame",mat), but turn to the QPixmap::scaled: Pixmap is a null pixmap after I removed the imshow("frame",mat). Even trying to lock the thread not solve the problem. I have done some search, found that it might due to QPixmap resource should be defined in something like "xxx.qrc".
My code is as below:
void IntelligentSurveillance::on_pushButton_clicked(){
QMutex processingMutex;
string filename = VIDEO_PATH;
VideoCapture cap;
cap.open(filename);
Mat mat;
QImage qImage;
for (;;)
{
cap >> mat;
//processingMutex.lock();
qImage = MatToQImage(mat);
//processingMutex.unlock();
ui.frame->setPixmap(QPixmap::fromImage(qImage).scaled(ui.frame->width(), ui.frame->height(), Qt::IgnoreAspectRatio));
//imshow("frame", mat);
if (waitKey(30) >= 0) break;
}}
Output always like: QPixmap::scaled: Pixmap is a null pixmap
The problem is that it works fine when I add imshow("frame",mat)...
Can anyone give some help? Thanks!!
You need to use temp variable to save image, for example
QPixmap icon = QPixmap(":/img/" + iconFileName);
QPixmap tmp = icon.scaled(30, 30, Qt::KeepAspectRatio);
value = tmp;
And this message gone!

cannot access values of Mat returned from OpenCV functions

I'm using cv::imread to load a image and do some processes with that image,
but I don't know why I can't read values of the returned Mat from imread function.
I used Mat.at method:
Mat iplimage = imread("Photo.jpg",1); //input
for(int i=0;i<iplimage.rows;i++){
for(int j=0;j<iplimage.cols;j++){
cout<<(int)iplimage.at<int>(i,j)<<" ";
}
cout<<endl;
}
But it appeared an error:
OpenCV Error: Assertion failed ( dims <= 2 && data && (unsigned)i0 <
(unsigned)size.p[0] &&
(unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
((((Sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) -1))*4) & 15)
== elemSize1()) is unknown function, file: "c:\opencv2.2\include\opencv2\core\mat.hpp", line 517
But it is ok if I use the direct access way:
Mat iplimage = imread("Photo.jpg",1); //input
for(int i=0;i<iplimage.rows;i++){
for(int j=0;j<iplimage.cols;j++){
cout<<(int)iplimage.data[i*iplimage.cols + j]<<" ";
}
cout<<endl;
}
Could anyone tell me how can I use the Mat.at method to access the above Mat?
Thanks for your help!
See this answer. In your case, the returned Mat is 3 dimensional, hence iplimage.at<int> fails the assertion, you just need to access the intensities in each channel like the way the mentioned answer explain.
you are trying to load with 3 channel of image it will be fine if you change to this Mat iplimage = imread("Photo.jpg",0); //input
I found the solution. It is because I used :
inputImage.at<int>(i,j) or inputImage.at<float>(1,2)
instead of, (int)inputImage.at<uchar>(1,2) or (float)inputImage.at<uchar>(1,2)
Sorry for my carelessness!
Mat iplimage = imread("Photo.jpg",1) this read in a 3 channel colour image. You can use Mat iplimage = imread("Photo.jpg",0) to read in the image as greyscale so that your iplimage.at(i,j) would work. Please note that you should use .at if your image is 8bit instead of .at.
If your image is not 8bit, you should use iplimage = imread("Photo.jpg",CV_LOAD_IMAGE_ANYDEPTH)

Resources