OpenCV cvCvtColor not linked? - opencv

I'm new to openCV, and I finally got it to open a window and show the webcam output.
I'm now trying to convert frames to greyscale before displaying them. Unfortunately, I get the following error every time I try to compile. error: ‘cvCvtColor’ was not declared in this scope I'm using the following line to compile
g++ main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o camera
Am I linking my code incorrectly? Here's the full code listing. Sorry for my formatting issues!
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <unistd.h>
IplImage* getCameraFrame(CvCapture* &camera)
{
IplImage *frame;
int w, h;
// If the camera hasn't been initialized, then open it.
if (!camera) {
printf("Acessing the camera ...\n");
camera = cvCreateCameraCapture( 0 );
if (!camera) {
printf("Couldn't access the camera.\n");
exit(1);
}
// Try to set the camera resolution to 320 x 240.
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, 240);
// Get the first frame, to make sure the camera is initialized.
frame = cvQueryFrame( camera );
if (frame) {
w = frame->width;
h = frame->height;
printf("Got the camera at %dx%d resolution.\n", w, h);
}
// Wait a little, so that the camera can auto-adjust its brightness.
sleep(1); // (in milliseconds)
}
// Wait until the next camera frame is ready, then grab it.
frame = cvQueryFrame( camera );
if (!frame) {
printf("Couldn't grab a camera frame.\n");
exit(1);
}
return frame;
}
int main(int argc, char* argv[])
{
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture( 0 );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvCvtColor(frame, frame, CV_RGB2GRAY);
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}

you're using the headers for the c++ api, but code from the deprecated c-api ( please don't !! ).
cv::VideoCapture cap(0);
while(cap.IsOpened()) {
cv::Mat f,g;
if ( ! cap.read(f) ) break;
cv::cvtColor(f, g, CV_RGB2GRAY);
cv::imshow("lalala",g);
if ( waitKey(10) == 27 ) break;
}
return 0;

Related

capture’ was not declared in this scope capture = cvCaptureFromCAM( 0 ); how to fix it

I was following a tutorial on web for an OpenCV library to detect eyes
when i compile it this error appears I tried to fix it but didn't find a
solution.
main.cpp:59:1: error: ‘capture’ was not declared in this scope
capture = cvCaptureFromCAM( 0 );
The code is long I put only the part that I think causing this error .
: Full code
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <math.h>
#include "constants.h"
#include "findEyeCenter.h"
#include "findEyeCorner.h"
/** Function Headers */
void detectAndDisplay( cv::Mat frame );
int main( )
{
cv::Mat frame;
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };
createCornerKernels();
ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);
capture = cvCaptureFromCAM( 0 );
if( capture)
{
while( true )
{
frame = cvQueryFrame( capture );
// mirror it
imshow("Video",frame);
cv::flip(frame, frame, 1);
frame.copyTo(debugImage);
// Apply the classifier to the frame
if( !frame.empty() ) {
detectAndDisplay( frame );
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
imshow(main_window_name,debugImage);
int c = cv::waitKey(10);
if( (char)c == 'c' ) { break; }
if( (char)c == 'f' ) {
imwrite("frame.png",frame);
}
}
}
releaseCornerKernels();
return 0;
}
}
try to use VideoCapture capture(0);

Video reading in opencv

I am new to opencv .I am trying to read video mp4 file but my code always says cannot open video device or file. I have placed video file in code folder .
VideoCapture capture;
capture.open("a.mp4"); // Open file
if (!capture.isOpened())
{
cout << "Cannot open video device or file!" << endl;
getch();
return -1;
}
Mat frame;
namedWindow("video", CV_WINDOW_AUTOSIZE);
while(true)
{
capture >> frame;
if (frame.empty())
break;
imshow("video", frame);
if (waitKey(30) == 'q')
break;
}
I am posting this answer in case someone came across this question as I did.
I was facing almost the same problem, trying to open a video file. My code worked with .avi but failed to open files with .mp4 files.
Turns out openCV needs ffmpeg decoder to be installed on the system. In case of windows OS, you will need to copy opencv_ffmpeg2411.dll file into C:\Windows\System32 directory. The DLL different systems can be found in OpenCV extracted directory.
Hope this helps someone.
this is correct code for video and image reading. I don't know what was the exact error. But using different code from orieally help me do my task in a different way.
// newproject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Image Reading
IplImage* img = cvLoadImage( "b.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
//Video Reading
// cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
// CvCapture* capture = cvCreateFileCapture( "video.avi" );
// IplImage* frame;
/* while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;//if user enter escape key then exit
}*/
//Summarize by sampling
CvCapture* capture = 0;
capture = cvCreateFileCapture( "video.avi" );
if(!capture){
return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read
double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
//double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
CvVideoWriter *writer = cvCreateVideoWriter("Ayesha",CV_FOURCC('M','J','P','G'),fps,size);
int x=0;
while(1){
x++;
bgr_frame = cvQueryFrame( capture );
if(!bgr_frame)
break;
if(x==19||x==21){
cvShowImage("Example2",bgr_frame);
cvWriteFrame( writer, bgr_frame );
}
if(x==20){
cvShowImage("Example2",bgr_frame);
cvWriteFrame( writer, bgr_frame );
x=0;
}
char c=cvWaitKey(30);
if (c==27) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}

how to free the camera

How can I release the camera when application keeps on running. It is still in on condition. here is the code. I don't know how to release it
#include <cv.h>
#include <highgui.h>
main( int argc, char* argv[] ) {
int i=1;
CvCapture* capture = NULL;
capture = cvCreateCameraCapture( 0 );
IplImage *frames = cvQueryFrame(capture);
while(1) {
if (i==20)
cvReleaseCapture ( &capture );
char c = cvWaitKey(33);
if( c == 27 ) break;
i++;
}
return 0;
}
Your code isn't totally clear so I hope I'm understanding correctly, but I think what you want is something more like this...
#include <cv.h>
#include <highgui.h>
int main( int argc, char* argv[] )
{
int i=1;
CvCapture* capture = NULL;
capture = cvCreateCameraCapture( 0 );
IplImage *frame = cvQueryFrame(capture);
while(1)
{
// if we are on the 20th frame, quit.
if (i==20)
{
cvReleaseCapture ( &capture );
break;
}
// if the user types whatever key 27 corresponds to, quit.
char c = cvWaitKey(33);
if( c == 27 )
{
cvReleaseCapture ( &capture );
break;
}
// do you want to get the next frame? here.
frame = cvQueryFrame( capture );
i++;
}
return 0;
}
Your problem is that you are not breaking after releasing the capture, so you will continue in the loop with a released camera. Also, you had IplImage *frames instead of IplImage *frame. That will only point to a single frame at a time, so I figured renaming it would be helpful for you.

OpenCV cannot find video device

I cannot use webcam as input device for OpenCV 2.3.1 in Ubuntu 11.04, this code works fine on windows:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM(-1);
if( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
It returns "ERROR: capture is NULL "
I found the solution, I need to install libv4l-0 and libv4l-dev then compile OpenCV with USE_V4L=ON.

OpenCV 2: How to save a ROI

I am new to OpenCV. Currently, trying to load and save a defined ROI of an image.
For OpenCV 1.x, I got it working with the following function...
#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
void SaveROI(const CStringA& inputFile, const CStringA& outputFile)
{
if (ATLPath::FileExists(inputFile))
{
CvRect rect;
rect.x = 8;
rect.y = 90;
rect.width = 26;
rect.height = 46;
IplImage* imgInput = cvLoadImage(inputFile.GetString(), 1);
IplImage* imgRoi = cvCloneImage(imgInput);
cvSetImageROI(imgRoi, rect);
cvSaveImage(outputFile.GetString(), imgRoi);
cvReleaseImage(&imgInput);
cvReleaseImage(&imgRoi);
}
}
How can this be done with the OpenCV 2 or C++. I tried the following without a success, the whole image is saved.
void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
{
if (ATLPath::FileExists(inputFile))
{
cv::Mat imgInput = cv::imread(inputFile.GetString());
if (imgInput.data != NULL)
{
cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));
imgInput.copyTo(imgRoi);
cv::imwrite(outputFile.GetString(), imgRoi);
}
}
}
Any help or suggestion?
You just don't need to call copyTo:
void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
{
if (ATLPath::FileExists(inputFile))
{
cv::Mat imgInput = cv::imread(inputFile.GetString());
if (imgInput.data != NULL)
{
cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));
cv::imwrite(outputFile.GetString(), imgRoi);
}
}
}
In your version copyTo sees that imgInput is bigger then imgRoi and reallocates a new full-size matrix to make the copy. imgRoi is already a sub-image and you can simply pass it to any OpenCV function.
Here is some tested code for blending, cropping and saving new images.
You crop and then save that region in a new file.
#include <cv.h>
#include <highgui.h>
#include <math.h>
// alphablend <imageA> <image B> <x> <y> <width> <height>
// <alpha> <beta>
IplImage* crop( IplImage* src, CvRect roi){
// Must have dimensions of output image
IplImage* cropped = cvCreateImage( cvSize(roi.width,roi.height), src->depth, src->nChannels );
// Say what the source region is
cvSetImageROI( src, roi );
// Do the copy
cvCopy( src, cropped );
cvResetImageROI( src );
cvNamedWindow( "check", 1 );
cvShowImage( "check", cropped );
cvSaveImage ("style.jpg" , cropped);
return cropped;
}
int main(int argc, char** argv){
IplImage *src1, *src2;
CvRect myRect;
// IplImage* cropped ;
src1=cvLoadImage(argv[1],1);
src2=cvLoadImage(argv[2],1);
{
int x = atoi(argv[3]);
int y = atoi(argv[4]);
int width = atoi(argv[5]);
int height = atoi(argv[6]);
double alpha = (double)atof(argv[7]);
double beta = (double)atof(argv[8]);
cvSetImageROI(src1, cvRect(x,y,width,height));
cvSetImageROI(src2, cvRect(100,200,width,height));
myRect = cvRect(x,y,width,height) ;
cvAddWeighted(src1, alpha, src2, beta,0.0,src1);
cvResetImageROI(src1);
crop (src1 , myRect);
cvNamedWindow( "Alpha_blend", 1 );
cvShowImage( "Alpha_blend", src1 );
cvWaitKey(0);
}
return 0;
}

Resources