OpenCV imread 8BPP PNG image error - c++

I am trying to read an 8BPP PNG image using imread() in OpenCV and copy it into a larger matrix. This is the code:
Mat subimage = imread((directory + file).toStdString(), IMREAD_COLOR);
subimage.copyTo(whole(Rect(rect.left(), rect.top(),
rect.width(), rect.height())));
I have tried other flags like IMREAD_ANYCOLOR, IMREAD_ANYDEPTH, IMREAD_GRAYSCALE and IMREAD_UNCHANGED. None of them seem to work (subimage remains empty).
I am getting the folowing error:
OpenCV Error: Assertion failed (!fixedSize()) in cv::_OutputArray::release, file ..\..\..\sources\modules\core\src\matrix.cpp, line 1619

I finally figured out the error. However, it has nothing to do with imread. I am downloading the images and was trying to read them before the download is complete.

Related

Dlib face detection doesn't work with grayscale images

Do you know why dlib face detection doesn't work with grayscale images (python works pretty well with grayscale images)?
My code:
mFaceDetector = dlib::get_frontal_face_detector();
// image is opencv grayscale mat
dlib::array2d<unsigned char> img;
dlib::assign_image(img, dlib::cv_image<unsigned char>(image));
std::vector<dlib::rectangle> mRets = mFaceDetector(img);
How to make it work?
Your code is nothing wrong in my perspective. Mine is the same. You should check
The image is loaded correctly by using imshow() function
If it works with non-gray image and other image
If you are setting any scan_fhog_pyramid value to the detector
the mRets.size()

How to open ECW file with GDAL in OpenCV 3.0

As you know, GDAL is added to OpenCV version 3. I have a satellite ecw image and want to read and show it. I already try to use OpenCV sample named : gdal-image.cpp. it has a line for reading input image:
cv::Mat image = cv::imread(argv[1], cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
my problem : I set my ecw image as argv[1] but it doesnt work.
should I convert my image before?
any way to read ecw using GDAL?
For read ecw image, i suggest you use GDAL, you can download GDAL binary prebuild and ECW extension from here https://www.gisinternals.com/release.php.
For read the first band of image you can use this code:
GDALDataset* dataset = (GDALDataset*) GDALOpenEx("image.ecw", GDAL_OF_RASTER, NULL, NULL, NULL);
GDALRasterBand* band = dataset->GetRasterBand(1);
cv::Rect roi(x, y, w, h);
cv::Mat mat;
mat.create(roi.size(),CV_32F);
band->RasterIO( GF_Read, roi.x, roi.y, roi.width, roi.height, mat.data,
roi.width, roi.height, GDT_Float32, 0, 0);
You just have to be sure that OpenCV data type match the GDAL datatype and that ROI dimensions are ok for the raster size.
For more info it is good: enter link description here
To read ECW file using GDAL,its driver should be built into the gdal, maybe you should test the supported driver in opencv. If the author didn't add the ecw driver, then do it youself. Here is the page about gdal in OpenCV:http://code.opencv.org/issues/3725
OpenCV's code repository was moved to github, and I can't find this issue, but in github i find something related here.

how to convert a jpg image to pgm format in cpp

I want to covert a .jpg image to a .pgm image.The image is being obtained from a tcp socket which has live streaming by a OPENCV program.
In matlab I used the imread function to do it. How do I do it in cpp?
I am working in linux platform. Is there any function to do it in OPENCV?
can anyone help?
regards,
shiksha
Yes.
In OpenCV, you can use imread() to load the JPG image, and then use imwrite() it to the PGM image (by using the CV_IMWRITE_PXM_BINARY format flag).
please look at OpenCV documentation for HighGui library functions cv::imread and cv::imwrite.
Read the jpg using cv::imread and resave it with cv::imsave using filename with proper extension.
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imwrite

bad_alloc exception when using imwrite to write ppm file in Opencv/C++ (Windows)

I am simply trying to save a Mat as a ppm image. I am able to read the ppm and load it into a Mat. The part of code that is causing the error is:
// Display Image
namedWindow("Comparison", CV_WINDOW_NORMAL);
imshow("Comparison", comparisonMat);
waitKey();
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PXM_BINARY);
compression_params.push_back(1);
imwrite("Comparison.ppm",comparisonMat,compression_params);
It builds successfully but when it comes to writing the file, I get the following error:
"Unhandled exception at 0x76b2c41f in HW.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0018f280.."
Any idea how to fix this?
Edit:
I made it work by doing the following:
IplImage* writeImage=cvCloneImage(&(IplImage)comparisonMat);
cvSaveImage("Comparison.ppm", writeImage);

converting a UYVY FFmpeg

I want to read and show a video using opencv. I've recorded with Direct-show, the Video has UYVY (4:2:2) codec, since opencv can't read that format, I want to convert the codec to an RGB color model, I readed about ffmpeg and I want to know if it's possible to get this done with it ? if not if you a suggestion I'll be thankful.
As I explained to you before, OpenCV can read some formats of YUV, including UYVY (thanks to FFmpeg/GStreamer). So I believe the cv::Mat you get from the camera is already converted to the BGR color space which is what OpenCV uses by default.
I modified my previous program to store the first frame of the video as PNG:
cv::Mat frame;
if (!cap.read(frame))
{
return -1;
}
cv::imwrite("mat.png", frame);
for(;;)
{
// ...
And the image is perfect. Executing the command file on mat.png reveals:
mat.png: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced
A more accurate test would be to dump the entire frame.data() to the disk and open it with an image editor. If you do that keep in mind that the R and B channels will be switched.