I am trying to make a simple conversion of an image from RGB to Grayscale with OpenCV. I am using dev-cp on windows, here is the code:
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
char* imageName = argv[1];
Mat image;
image = imread( imageName );
Mat gray_image;
cvtColor(image,gray_image,CV_RGB2GRAY);
}
When I execute it, seems it crashes on the cvtColor.
please try
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
if (argc < 2) return 0;
char* imageName = argv[1];
Mat image;
image = imread( imageName );
if(image.empty()) return 0;
Mat gray_image;
cvtColor(image,gray_image,CV_RGB2GRAY);
imshow("image",image);
cv::waitKey(0);
return 0;
}
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 would like to ask question about on how to export/write all the pixel values into a txt file or other format that can be opened by notepad for example. Below the program.
Thanks, HB
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <stdlib.h>
#include<fstream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
IplImage *img = cvLoadImage("MyImg.png");
CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );
cvConvert( img, mat );
outFile.open("MyFile.txt");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
/// Get the (i,j) pixel value
CvScalar scal = cvGet2D( mat,j,i);
printf( "(%.f,%.f,%.f)",scal.val[0], scal.val[1],scal.val[2] );
}
printf("\n");
}
waitKey(1);
return 0;
}
The class Mat of the new OpenCV C++ API is preferred over IplImage because it simplifies your code: read more about the class Mat. For more information about loading an image, you could read Load, Modify, and Save an Image.
In order to write a text file using C++, you could use the class ofstream
Here is the source code.
#include <opencv2/opencv.hpp>
using namespace cv;
#include <fstream>
using namespace std;
int main( int argc, char** argv )
{
Mat colorImage = imread("MyImg.png");
// First convert the image to grayscale.
Mat grayImage;
cvtColor(colorImage, grayImage, CV_RGB2GRAY);
// Then apply thresholding to make it binary.
Mat binaryImage(grayImage.size(), grayImage.type());
threshold(grayImage, binaryImage, 128, 255, CV_THRESH_BINARY);
// Open the file in write mode.
ofstream outputFile;
outputFile.open("MyFile.txt");
// Iterate through pixels.
for (int r = 0; r < binaryImage.rows; r++)
{
for (int c = 0; c < binaryImage.cols; c++)
{
int pixel = binaryImage.at<uchar>(r,c);
outputFile << pixel << '\t';
}
outputFile << endl;
}
// Close the file.
outputFile.close();
return 0;
}
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;
}