Resizing Image leads to Segmentation Fault OpenCV C++ - c++

I am working on a OpenCV project in C++. In this I am trying to read an image and then resize the image, but on resizing the image, I get Segmentation Fault.
I am using Ubuntu 20.04 and install OpenCV 4.5.4 following this tutorial: https://linuxize.com/post/how-to-install-opencv-on-ubuntu-20-04/
Here is the code I am using:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
// This works: Printing out the OpenCV version
cout << "OpenCV version : " << CV_VERSION << endl;
cout << "Major version : " << CV_MAJOR_VERSION << endl;
cout << "Minor version : " << CV_MINOR_VERSION << endl;
cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
// This Works: Read the image using imread function
Mat image = imread("./test_image.jpg");
cv::Mat dst;
// This is where it fails.
cv::resize(image, dst, cv::Size(150,150));
cv::namedWindow("Source", cv::WINDOW_AUTOSIZE );
cv::imshow("Source", image);
cv::namedWindow("resize", cv::WINDOW_AUTOSIZE );
cv::imshow("resize", dst);
waitKey(0);
return 0;
}
I am able to show the loaded image/video frame before resizing.
Can someone please help me here as to where I am going wrong? I have been stuck on this for past 2 days, tried almost all tutorials and solutions available online, but nothing worked. Thanks.

First of all check if the image is empty or not using image.empty().
If that's not the case, then it's probably a issue with OpenCV. I faced the same problem with OpenCV 4.2. Updating the OpenCV version might solve the problem. OpenCV Version 4.6 perfectly worked for me.

Related

OpenCV C++ VideoCapture on MacOS Monterey Not working

Hi I'm using M1 Macbook Pro 2021 with Monterey OS.
I've been trying to use my mac's internal webcam with OpenCV C++ VideoCapture class on Visual Studio Code, but i keep getting this weird errors. I've given both terminal and iTerm access to the Camera on my Mac's Preferences, but it still keeps giving me this error.
This is my Code,
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void camera_in()
{
VideoCapture cap;
cap.open(2, CAP_AVFOUNDATION);
if (!cap.isOpened())
{
cerr << "Camera open failed!" << endl;
return;
}
cout << "Frame width: " << cvRound(cap.get(CAP_PROP_FRAME_WIDTH)) << endl;
cout << "Frame height: " << cvRound(cap.get(CAP_PROP_FRAME_HEIGHT)) << endl;
Mat frame, inversed;
while (true)
{
cap >> frame;
if (frame.empty())
break;
inversed = ~frame;
imshow("frame", frame);
imshow("inversed", inversed);
if (waitKey(10) == 27)
break;
}
destroyAllWindows();
}
int main()
{
camera_in();
}
And this is the error i get from executing it.
2022-08-05 18:15:01.284398+0900 video[7664:45504] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10b54c320> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-08-05 18:15:01.291647+0900 video[7664:45504] HALC_ProxyObjectMap::_CopyObjectByObjectID: failed to create the local object
2022-08-05 18:15:01.291664+0900 video[7664:45504] HALC_ShellDevice::RebuildControlList: couldn't find the control object
2022-08-05 18:15:01.316885+0900 video[7664:45504] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10c50bb40> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
I ran this code on my macbook pro m1 14" and it was working, I had to change:
cap.open(2, CAP_AVFOUNDATION);
to:
cap.open(0, CAP_AVFOUNDATION);
for it to work though (0 is the index of the built in webcam).

Different Tesseract result for Mat and Pix

Goal
Getting the same quality result when using OpenCV Mat as when using Leptonica Pix when doing OCR with Tesseract.
Environment
C++17, OpenCV 3.4.1, Tesseract 3.05.01, Leptonica 1.74.4, Visual Studio Community 2017, Windows 10 Pro 64-bit
Description
I'm working with Tesseract and OCR, and have found what I think is a peculiar behaviour.
This is my input image:
And this is my code:
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace cv;
using namespace tesseract;
void opencvVariant(string titleFile);
void leptonicaVariant(const char* titleFile);
int main()
{
cout << "Tesseract with OpenCV and Leptonica" << endl;
const char* titleFile = "raptor-companion-2.jpg";
opencvVariant(titleFile);
leptonicaVariant(titleFile);
cout << endl;
system("pause");
return 0;
}
void opencvVariant(string titleFile) {
cout << endl << "OpenCV variant..." << endl;
TessBaseAPI ocr;
ocr.Init(NULL, "eng");
Mat image = imread(titleFile);
ocr.SetImage(image.data, image.cols, image.rows, 1, image.step);
char* outText = ocr.GetUTF8Text();
int confidence = ocr.MeanTextConf();
cout << "Text: " << outText << endl;
cout << "Confidence: " << confidence << endl;
}
void leptonicaVariant(const char* titleFile) {
cout << endl << "Leptonica variant..." << endl;
TessBaseAPI ocr;
ocr.Init(NULL, "eng");
Pix *image = pixRead(titleFile);
ocr.SetImage(image);
char* outText = ocr.GetUTF8Text();
int confidence = ocr.MeanTextConf();
cout << "Text: " << outText << endl;
cout << "Confidence: " << confidence << endl;
}
The methods opencvVariant and leptonicaVariant is basically the same except that one is using the class Mat from OpenCV and the other Pix from Leptonica. Yet, the result is quite different.
OpenCV variant...
Text: Rapton
Confidence: 68
Leptonica variant...
Text: Raptor Companion
Confidence: 83
As one can see in the output above, the Pix variant gives a much better result than the Mat variant. Since my code relies heavily on OpenCV for the computer vision before the OCR its essential for me that the OCR works well with OpenCV and its' classes.
Questions
Why does Pix give a better result than Mat, and vice versa?
How could the algorithm be changed to make the Mat variant as efficient as the Pix variant?
OpenCV imread function by default reads image as colored, which means you get pixels as BGRBGRBGR....
In your example you are assuming opencv image is grayscale, so there are 2 ways of fixing that:
Change your SetImage line according to number of channels in opencv image
ocr.SetImage((uchar*)image.data, image.size().width, simageb.size().height, image.channels(), image.step1());
Convert your opencv image to grayscale with 1 channel
cv::cvtColor(image, image, CV_BGR2GRAY);

Build OpenCV with OpenCL support

in CMake, I built OpenCV with OpenCL Enable ON(It automatically detected the OPENCL_INCLUDE_DIR path but the OPENCL_LIBRARY was empty, even after clicking config. for OPENCL_LIBRARY i don't see browse button either .. after generating opencv binaries then i run the below code
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
int main()
{
if (!cv::ocl::haveOpenCL())
cout << "OpenCL is not avaiable..." << endl;
else cout << "OpenCL is AVAILABLE! :) " << endl; //this is the output
cv::ocl::setUseOpenCL(true);
cout << context.ndevices() << " GPU devices are detected." << endl;
for (int i = 0; i < context.ndevices(); i++)
{
cv::ocl::Device device = context.device(i);
cout << "name: " << device.name() << endl;
cout << "available: " << device.available() << endl;
cout << "imageSupport: " << device.imageSupport() << endl;
cout << "OpenCL_C_Version: " << device.OpenCL_C_Version() << endl;
cout << endl;
} //this works & i can see my video card name & opencl version
cv::ocl::Device(context.device(0));
}
When i make use of UMat to measure the performance, the performance with(UMat) or without(Mat) OpenCL did not make any difference.
I downloaded AMD-APP-SDK from this link and tried to build but there was no OpenCL binaries (instead i saw opengl dll files[glew32.dll & glut32.dll]. How do i build OpenCV with OpenCL by linking the OPENCL_LIBRARY?
I believe you have OpenCL, hence the result of your call to haveOpenCL and from the version request. I'm not sure the results of your performance test equate that you don't have OpenCL.
If you want to understand OpenCL, I would take a step back and figure it out first and then try to understand OpenCV with it.
Your link didn't work, did you try this. It has a link to the current AMD APP SDK (3.0) I would go through that setup and make sure you can make the OpenCL samples build/work on your system and then you should be able to troubleshoot why it isn't working in OpenCV (if it truly isn't).
As to performance, well, it depends. Every time you send data to and from the the graphics card it comes at a cost; the Transparent API was designed to make that choice for you: if sending it to the card for faster processing is worth the trip there and back... if it is not worth the trip you will actually have poorer performance. Additionally, not all of the library will run on the GPU. See some of the explanation on opencv.org.

Raspberry Pi CGI script with OpenCV not saving images

I'm trying to create a CGI script that will take a picture and save it to the location I give it. I'm using the Raspberry Pi and the Pi camera module with the uv4l driver. I have also chosen to use Apache2.
Currently the script runs with no errors given and no errors in the Apache error log, but the image doesn't get saved. The camera does open because the red light appears on it. I also check to see if the image is empty which it isn't.
So far I have tried changing folder permissions so that the user pi owns everything. I have also tried to save over an already existing file but it never gets updated. I have never used Apache2 or CGI scripting before so I feel that the problem lies in there but I'm not entirely sure what to search because I am getting no errors. Any suggestions would be greatly appreciated.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv){
cv::VideoCapture cap(-1);
if (!cap.isOpened()){
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Camera didn't open </h1>";
cout << "</html>";
return -1;
}
//cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
//cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
int count = 40;
cv::Mat frame;
bool bSuccess = cap.read(frame);
while (count != 0){
count--;
}
if (!bSuccess){
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Photo did't work get read in</h1>";
cout << "</html>";
return 0;
}
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Photo Taken + Saved</h1>";
cout << "</html>";
cv::imwrite("/var/www/photos/Current.png", frame);
return 0;
}
I'm using this command to compile:
g++ -lopencv_core -lopencv_highgui -L/usr/lib/uv4l/uv4lext/armv6l -luv4lext -Wl,- rpath,'/usr/lib/uv4l/uv4lext/armv6l' time.cpp -o test_script.cgi
I fixed my own problem. The method imwrite() was saving over an already existing image without write permissions.

opencv: using cout with Mat object throwing exception

I needed to print my Mat object and the programm throws an exception... The project is really simply: creating Mat object and printing by using cout - just like in OpenCV tutorial:
#include <core/core.hpp>
#include <highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat O = Mat::ones(2, 2, CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
// Point2f P(5, 1);
// cout << "Point (2D) = " << P << endl << endl;
return 0;
}
The exception says: Unhandled exception at 0x59430671 (msvcp100d.dll) in printingTest.exe: 0xC0000005: Access violation reading location 0x00000000. Console shows only
O = [
Precisely it stops on "operations.hpp" at:
static inline std::ostream& operator << (std::ostream& out, const Mat& mtx) { Formatter::get()->write(out, mtx); return out; }
It seems "out" to be empty, but does someone know why? Tutorial says it should work...
I had earlier similar problem with throwing exception and I solved it here:
http://answers.opencv.org/question/5113/problem-with-reading-image-to-mat/
Is it possible that there is another environmental variable conflict? or maybe a collision 'cause I'm using VS2012 and there's OpenCV only for v10?
The thing with Point2f which is commented works normally.
Your code works fine here with VS2010. Make sure you are linking the correct libraries (release vs. debug). If this does not help try to reinstall openCV or make your own build on VS2012, preparing it with cmake, from the sources.