I use opencv2.4.9 whith visual studio 2010
when I have to show an image program display the empty window and not show image
and its my code:
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\core\core.hpp>
#include<opencv\cv.h>
int main(int argc,char**argv[]) {
IplImage* img1=cvLoadImage("C:\opencv\sources\samples\cpp\board.jpg");
cvNamedWindow("img1",CV_WINDOW_AUTOSIZE);
cvShowImage("img1",img1);
cvWaitKey(0);
cvReleaseImage(&img1);
}
The problem is in the way you wrote the path of the image. you should not use the escape character alone. You can solve it by one of these:
IplImage* img1=cvLoadImage("C:\\opencv\\sources\\samples\\cpp\\board.jpg");
Or:
IplImage* img1=cvLoadImage("C:/opencv/sources/samples/cpp/board.jpg");
Or:
IplImage* img1=cvLoadImage(R"(C:\opencv\sources\samples\cpp\board.jpg)");
BTW, you are using C interface which is really too out of data. If you do not have a REAL reason to use it, please do not. The equivalent code that use C++ is:
int main(int argc,char**argv[]) {
cv::Mat img1=cv::imread("C:\\opencv\\sources\\samples\\cpp\\board.jpg");
cv::namedWindow("img1",CV_WINDOW_AUTOSIZE);
cv::imshow("img1",img1);
cv::waitKey(0);
//No need to release manually
}
if you use Mat then you can use:
if (frame1.empty())
{
std::cout << "no image";
break;
}
then if the image not empty you should use more delay to display the images.
for example you can use:
waitKey(100);
Related
I'm using OpenCV 3.2.0 compiled with Qt support and function cv::addText to put text on image. Here's the simplest code that reproduces the error
#include <opencv/cv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image, resized;
image = imread(argv[1], IMREAD_COLOR); // Read the file
namedWindow("test", 1);
addText(image, "SomeText", Point(5, 27), fontQt("Times"));
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window", image); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I get the following error
/home/vitaly/CLionProjects/opencvTest/cmake-build-debug/opencvTest
/home/vitaly/Pictures/img.jpg OpenCV Error: Null pointer (NULL
guiReceiver (please create a window)) in cvAddText, file
/home/vitaly/Documents/opencv/opencv/modules/highgui/src/window_QT.cpp,
line 114 terminate called after throwing an instance of
'cv::Exception' what():
/home/vitaly/Documents/opencv/opencv/modules/highgui/src/window_QT.cpp:114:
error: (-27) NULL guiReceiver (please create a window) in function
cvAddText
which goes away if I add
namedWindow("test", WINDOW_AUTOSIZE);
before addText.
However, I cannot understand why would qt or opencv need an opened window for that ? I don't need to display the image, I'm only using it to put text on image and then save it, I don't want to create any windows.
So here's my questions
Why window is required for that ?
Is there a way around it ? (To not create windows)
As you can see in the source code:
CV_IMPL void cvAddText(const CvArr* img, const char* text, CvPoint org, CvFont* font)
{
if (!guiMainThread)
CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" );
QMetaObject::invokeMethod(guiMainThread,
"putText",
autoBlockingConnection(),
Q_ARG(void*, (void*) img),
Q_ARG(QString,QString::fromUtf8(text)),
Q_ARG(QPoint, QPoint(org.x,org.y)),
Q_ARG(void*,(void*) font));
}
addText requires a gui thread. as the actual drawing happens in that thread. No thread, no function, no drawing...
Why is it like that? Well because it made sense to somebody I guess.
It's open source, feel free to code your own workaround. Otherwise use OpenCV's putText.
puttext should work fine as already suggested.
If you're looking for more powerful gui formatting options, you can use the CanvasCV library which blends into the OpenCV main loop.
Here is a tutorial about using its Text widget.
Here is another one about auto centering text with layouts.
I'm trying to use this function:
fastNlMeansDenoising(image, image, 3.0, 7, 21);
Using OpenCV with Visual Studio 2010 express, but it said "identifier not found".
I did a quick search and found that this must be a ".lib" is missing, but I did not find which library should I add in my project for this function to work. Anyone could help me with this?
Ok. In order to use fastNlMeansDenoising(image, image, 3.0, 7, 21);
1) You need to configure opencv 2.4.8 or 2.4.9.
Here is procedure to link opencv 249 with Visual studio.
2) Use the following code to test opencv function
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
// load the image
Mat img = imread("lenna.jpg");
if(!img.data)
{
cout << "File not found" << endl;
return -1;
}
// show it in a window
namedWindow( "Image", WINDOW_AUTOSIZE );
imshow("Image", img);
// image window will immediately disappear if the program ends, so
// we'll wait for a keypress, indefinitely
waitKey();
// do a simple transformation: convert to grayscale
// first copy the image
Mat img_gray = img.clone();
Mat img1;
cvtColor(img, img_gray, CV_RGB2GRAY);
fastNlMeansDenoising(img_gray,img1,3.0,7,21);
imshow("Image", img1);
waitKey();
return 0;
}
Hope, this helps you.
Cheers,
The function is defined in the photo.hpp file. So you have to get the opencv_photo300.lib
Edit 1:
I searched a little bit (sorry im at work, dont have more time) and i couldnt find the library itself. You can go ahead and build opencv yourself from: https://github.com/Itseez/opencv
Then you can just search that folder for the lib.
An installationguide for the build process is here: http://docs.opencv.org/trunk/doc/tutorials/introduction/windows_install/windows_install.html
Edit 2:
Berak is right, the opencv_photo300.lib is not in the 2.3 Version of OpenCV. Update your OpenCV to the current version 2.4.9 and you'll have what you need.
you will have to use opencv 2.4.9, it is not available in 2.3.0
I tried to convert RGB image into grayscale, conversion is successfull and i can display it with imshow. Yet, i cannot write it with imwrite, imwrite function returns NULL. My original image is 1920*1080 JPEG file with UINT8. Here is the code
#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main(int argc, char** argv)
{
char* imageName = argv[1];
Mat image;
image = imread(imageName, 1);
if (argc != 2 || !image.data)
{
printf(" No image data \n ");
return -1;
}
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
if (imwrite("../../images/Gray_Image.jpg", gray_image) ==NULL) {
printf( "Writing image is not successfull!");
}
namedWindow(imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow(imageName, image);
imshow("Gray image", gray_image);
waitKey(0);
return 0;
}
Why imwrite returns NULL?
The #include directive supports forward slashes. Use forward slashes there, for portability. And for sanity.
File handling functions generally don't support forward slashes in Windows. Use backward slashes there. Or, better, use some path handling class (such classes often support forward slashes, since they need to be portable, and automatically convert to backward slash in Windows).
In short,
/ → \ to fix the immediate problem.
That said, the OpenCV imwrite probably does not support automatic folder creation, so you'd better make sure that …
the specified folder exists, and
for a relative path (as you have), that the program execution's current folder is where you imagined the relative path starting.
Good day everyone! So currently I'm working on a project with video processing, so I decided to give a try to OpenCV. As I'm new to it, I decided to find few sample codes and test them out. First one, is C OpenCV and looks like this:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
int main( void ) {
CvCapture* capture = 0;
IplImage *frame = 0;
if (!(capture = cvCaptureFromCAM(0)))
printf("Cannot initialize camera\n");
cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
while (1) {
frame = cvQueryFrame(capture);
if (!frame)
break;
IplImage *temp = cvCreateImage(cvSize(frame->width/2, frame->height/2), frame->depth, frame->nChannels); // A new Image half size
cvResize(frame, temp, CV_INTER_CUBIC); // Resize
cvSaveImage("test.jpg", temp, 0); // Save this image
cvShowImage("Capture", frame); // Display the frame
cvReleaseImage(&temp);
if (cvWaitKey(5000) == 27) // Escape key and wait, 5 sec per capture
break;
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
return 0;
}
So, this one works perfectly well and stores image to hard drive nicely. But problems begin with next sample, which uses C++ OpenCV:
#include "opencv2/opencv.hpp"
#include <string>
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
//namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_RGB2XYZ);
imshow("edges", edges);
//imshow("edges2", frame);
//imwrite("test1.jpg", frame);
if(waitKey(1000) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
So, yeah, generally, in terms of showing video (image frames) there is practically no changes, but when it comes to using im** functions, some problems arise.
Using cvSaveImage() works out nicely, but the moment I try to use imwrite(), unhandled exception arises in regards of 'access violation reading location'. Same goes for imread(), when I'm trying to load image.
So, the thing I wanted to ask, is it possible to use most of the functionality with C OpenCV? Or is it necessary to use C++ OpenCV. If yes, is there any solution for the problem I described earlier.
Also as stated here, images initially are in BGR-format, so conversion needed. But doing BGR2XYZ conversion seems to invert colors, while RGB2XYZ preserve them. Examples:
images
Or is it necessary to use C++ OpenCV?
No, there is no necessity whatsoever. You can use any interface you like and you think you are good with it (OpenCV offers C, C++, Python interfaces).
For your problem about imwrite() and imread() :
For color images the order channel is normally Blue, Green, Red , this
is what imshow() , imread() and imwrite() expect
Quoted from there
I have searched a lot about my simple problem but I didn't find solution. When I run my code black console shows me the camera frame size but in the window video is not showing, it shows a solid gray screen. But if I play a video from HDD then it works fine.
Please help me some one.
This is my code
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv){
CvCapture *capture;
IplImage* img=0;
cvNamedWindow("Window");
capture = cvCreateCameraCapture( -1);
//capture = cvCaptureFromAVI("1.mp4");
//capture = cvCaptureFromCAM(-1);
int ext=0;
assert( capture );
if(capture==NULL){
cout<<"Cam Not Found!!!"<<endl;
getchar();
return -5;
}
while ( true ){
img = cvQueryFrame( capture );
cvSaveImage("1.jpg",img);
if (!img){
printf("Image not Found\n");
break;
}
cvShowImage("Window", img);
cvWaitKey(50);
}
cvReleaseImage(&img);
cvDestroyWindow("Window");
cvReleaseCapture(&capture);
return 0;
}
I use opencv 2.2 and Visual studio 2010
One thing is obviouslly wrong, you need to change the order of the calls to:
cvShowImage("Window", img);
cv::waitKey(20);
Second, it's essential that you check the success of cvQueryFrame():
img = cvQueryFrame( capture );
if (!img)
{
// print something
break;
}
EDIT:
By the way, I just noticed you are mixing the C interface of OpenCV with the C++ interface. Don't do that! Replace cv::waitKey(50); by cvWaitKey(50);.
For debugging purposes, if cvQueryFrame() succeeds I suggest you store one frame to the disk with cvSaveImage(), and if that image is OK it means the capture procedure is actually working perfectly and the problem is somewhere else.
I jast switch the openCV version 2.2 to 2.1 and its work perfectly.......
I am using OpenCV version 3.1, I got the same problem, I re-built openCV 3.1 and re-checked Environment Variables, so my problem resolved. You can back-up built-opencv and extract if you need. Sorry for my bad english :)