i am reading the Learning CV book, i came across the first example and encounter this problem
Using OPENCV 3.0.0 and VS 2013, all libraries added and checked.
the code is as follows
#include "opencv2/highgui/highgui.hpp"
int main( int argc, char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
So after compiling or build, I got a window named Example1, and it is grey, no image in the window.
Is this correct? Or what should I expect to get?
You are not loading the image correctly, i.e. argv[1] has an invalid path. You can check this like:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
//IplImage* img = cvLoadImage("path_to_image");
if (!img)
{
std::cout << "Image not loaded";
return -1;
}
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
You can supply the path also directly in the code like:
IplImage* img = cvLoadImage("path_to_image");
You can refer here to know why your path may be wrong.
You also shouldn't use old C syntax, but use the C++ syntax. Your example will be like:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
int main()
{
Mat3b img = imread("path_to_image");
if (!img.data)
{
std::cout << "Image not loaded";
return -1;
}
imshow("img", img);
waitKey();
return 0;
}
You can refer to this answer to know how to setup Visual Studio correctly.
Its not clear if the image is being loaded. OpenCV will silently fail if it can't find the image.
Try
auto img= cv::imread(name, CV_LOAD_IMAGE_ANYDEPTH);
if (img.data() == nullptr)
{
std::cout << "Failed to load image" << std::endl;
}
Related
Im relativly new to OpenCV. In this case I tried to save an image using cvSaveImage after making some processing, but this error was thrown
Assertion failed ((flags & FIXED_TYPE) != 0) in cv::_InputArray::type, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix_wrap.cpp, line 807
It seems, like it has some problem with type of input array, but i have no idea why?.
Here is what my code look like
int main(int argc, char** argv) {
IplImage* img = cvLoadImage("HOLES_CAM1_NG.bmp", CV_LOAD_IMAGE_GRAYSCALE);
IplImage* houghImg = cvCloneImage(img);
/*
SOME PROCESSING
*/
cvSaveImage("HOLES_CAM1_NG_processed.png", houghImg);
cvReleaseImage(&img);
cvReleaseImage(&houghImg);
}
You are using the deprecated C API.
Please try doing something like this:
Reference: https://docs.opencv.org/2.4/doc/tutorials/introduction/load_save_image/load_save_image.html
#include <cv.h>
#include <highgui.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat img;
img = imread( "HOLES_CAM1_NG.bmp", CV_LOAD_IMAGE_GRAYSCALE );
if(!img.data )
{
printf( " No image data \n " );
return -1;
}
/*
SOME PROCESSING
*/
imwrite( "HOLES_CAM1_NG_processed.png", houghImg );
namedWindow( "Original image", CV_WINDOW_AUTOSIZE );
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
imshow( "Original image", img );
imshow( "Hough image", houghImg );
waitKey(0);
return 0;
}
If cvSaveImage() is not working, it would have been better to remove it like cvCopyImage :)
I am trying to read video file, but its throwing error.
The code is:
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char* argv[])
{
Mat inputVideo;
Mat frame;
Mat HSV;
Mat tracking;
char checkKey;
VideoCapture capture;
capture.open("video/input.mp4");
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
namedWindow("Original Video", WINDOW_AUTOSIZE );
while(1){
capture.read(inputVideo);
if (!inputVideo.empty())
{
imshow("Original Video",inputVideo);
}
waitKey(20);
}
return 0;
}
On running this code, the error I am getting is:
Unable to stop the stream: Inappropriate ioctl for device
(video_reading:3459): GLib-GObject-CRITICAL **: g_object_set:
assertion 'G_IS_OBJECT (object)' failed
I tried looking for solutions, but i did not get it. Can some one help me in solving this error.
I made little changes in your code. Try this:
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char* argv[])
{
Mat inputVideo;
Mat frame;
Mat HSV;
Mat tracking;
char checkKey;
VideoCapture capture;
capture.open("video/input.mp4");
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
namedWindow("Original Video", WINDOW_AUTOSIZE);
while (1) {
capture >> inputVideo;
if (inputVideo.empty())
break;
imshow("Original Video", inputVideo);
waitKey(1);
}
capture.release();
return 0;
}
I have found a program which can binarize an image and make the image monochrome
OpenCV Adaptive Threshold OCR
I have copy/pasted the code
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdarg.h>
#include "/usr/include/opencv2/opencv.hpp"
#include "fstream"
#include "iostream"
using namespace std;
using namespace cv;
void CalcBlockMeanVariance(Mat& Img,Mat& Res,float blockSide=21) // blockSide - the parameter (set greater for larger font on image)
{
Mat I;
Img.convertTo(I,CV_32FC1);
Res=Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
Mat inpaintmask;
Mat patch;
Mat smallImg;
Scalar m,s;
for(int i=0;i<Img.rows-blockSide;i+=blockSide)
{
for (int j=0;j<Img.cols-blockSide;j+=blockSide)
{
patch=I(Range(i,i+blockSide+1),Range(j,j+blockSide+1));
cv::meanStdDev(patch,m,s);
if(s[0]>0.01) // Thresholding parameter (set smaller for lower contrast image)
{
Res.at<float>(i/blockSide,j/blockSide)=m[0];
}else
{
Res.at<float>(i/blockSide,j/blockSide)=0;
}
}
}
cv::resize(I,smallImg,Res.size());
cv::threshold(Res,inpaintmask,0.02,1.0,cv::THRESH_BINARY);
Mat inpainted;
smallImg.convertTo(smallImg,CV_8UC1,255);
inpaintmask.convertTo(inpaintmask,CV_8UC1);
inpaint(smallImg, inpaintmask, inpainted, 5, INPAINT_TELEA);
cv::resize(inpainted,Res,Img.size());
Res.convertTo(Res,CV_32FC1,1.0/255.0);
}
int main( int argc, char** argv )
{
namedWindow("Img");
namedWindow("Edges");
//Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
Mat Img=imread("Test2.JPG",0);
Mat res;
Img.convertTo(Img,CV_32FC1,1.0/255.0);
CalcBlockMeanVariance(Img,res);
res=1.0-res;
res=Img+res;
imshow("Img",Img);
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
imwrite("result.jpg",res*255);
imshow("Edges",res);
waitKey(0);
return 0;
}
compile
g++ binarize.cpp `pkg-config opencv --cflags --libs`
run
./a.out
error
(Img:27277): Gtk-WARNING **: cannot open display:
update
int main( int argc, char** argv )
{
namedWindow("Img");
namedWindow("Edges");
//Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
Mat Img=imread("Test2.JPG",0);
Mat res;
Img.convertTo(Img,CV_32FC1,1.0/255.0);
CalcBlockMeanVariance(Img,res);
res=1.0-res;
res=Img+res;
imshow("Img",Img);
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
imwrite("result.tif",res*255);
imshow("Edges",res);
waitKey(0);
return 0;
}
Just built this code on ubuntu 14.03 x64, using the same command line as you used, no any messages, runs fine.
I think warning related to your system environment.
I'm trying to play a video file using the following code.
When run it only shows a black screen with the window name (Video), can anyone help me fix it.
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;
int main( int argc, char** argv )
{
CvCapture* capture = cvCreateFileCapture( "1.avi" );
Mat frame= cvQueryFrame(capture);
imshow("Video", frame);
waitKey();
cvReleaseCapture(&capture);
}
Try this if you only want to play a video ::::::::::::::::::::::::::
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
int main(int argc, char** argv)
{
cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
//CvCapture* capture = cvCreateFileCapture("20051210-w50s.flv");
CvCapture* capture = cvCreateFileCapture("1.wmv");
/* if(!capture)
{
std::cout <<"Video Not Opened\n";
return -1;
}*/
IplImage* frame = NULL;
while(1) {
frame = cvQueryFrame(capture);
//std::cout << "Inside loop\n";
if (!frame)
break;
cvShowImage("Example3", frame);
char c = cvWaitKey(33);
if (c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example3");
std::cout << "Hello!";
return 0;
}
Actually the code you posted won't even compile.
Just have a look at OpenCV documentation: Reading and Writing images and video
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Video",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Video", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
My task is to perform some operations on the roi of an image. But after performing these, I want the changes also to be made visible in the same region of the original image (in code called "image"), not just in the roi as seperate image (which is "image_roi2"). How could I achieve this?
My code looks like this:
Mat image;
Mat image_roi2;
float thresh;
Rect roi = Rect(x, y, widh, height);
Mat image_roi = image(roi);
threshold(image_roi, image_roi2, thresh, THRESH_TOZERO, CV_THRESH_BINARY_INV);
You just need an additional image_roi2.copyTo( image_roi );
Below is an entire example.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << " Usage: " << argv[0] << " imagem.jpg" << std::endl;
return -1;
}
cv::Mat image;
cv::Mat image_roi2;
image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
cv::Rect roi( 100, 100,200, 200);
cv::Mat image_roi = image( roi );
cv::threshold(image_roi, image_roi2, 250, 255, CV_THRESH_BINARY_INV );
image_roi2.copyTo( image_roi );
cv::namedWindow("Imagem", CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);
cv::resizeWindow("Imagem", 600, 400);
cv::imshow("Imagem", image); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}
I think this is what you want - image_roi.copyTo(image(roi));