1 | initial version |
hi there,
i want to do detection using usb camera and the problem is that camera frame is not working fine means i can not get continue frame, i tried following code to detect the contour and draw rectangle around it.
#include "opencv/cv.h"
using namespace cv; using namespace std;
int lowerH=168; int upperH=256;
int lowerS=175; int upperS=256;
int lowerV=0; int upperV=256;
IplImage* frame = 0 ; IplImage* frame2 = 0 ; IplImage* imgHSV = 0 ; IplImage* imgThresh = 0 ;
vector<vector<point> > contours; vector<vec4i> hierarchy;
void draw_conture(IplImage * temp) {
cv::Mat tempm(temp,true);
findContours( tempm , contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
vector<vector<cv::point> > contours_poly( contours.size() );
vector<rect> boundRect( contours.size() );
vector<float>area( contours.size() );
for( int i = 0; i < contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = boundingRect( Mat(contours_poly[i]) ) ; area[i]= contourArea(Mat(contours_poly[i]));
}
const CvScalar color=CV_RGB(0x00,0xFF,0x00);
for( int i = 0; i< contours.size(); i++ )
{
if (area[i]>1000)
rectangle(tempm, boundRect[i].tl(), boundRect[i].br(), color, 1, 1, 0 );
}
CvSeq* contour; //hold the pointer to a contour CvSeq* result; //hold sequence of points of a contour CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
cvFindContours(temp, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for (; contour != 0; contour = contour->h_next)
{
result = cvApproxPoly(contour, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);
CvRect rect = cvBoundingRect(contour, 0); //extract bounding box for current contour
if( cvContourArea(result, CV_WHOLE_SEQ,0) >= 1000 )
{
cvDrawContours(frame, contour, CV_RGB(0,0,255), CV_RGB(0,0,255), 2, 1, 8, cvPoint(0, 0));
frame2 = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
cvCopy(frame, frame2);
cvRectangle(frame2, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(0,0,255),2, 8, 0);
cout<< "height =" << rect.height <<endl; cout<<="" "width=" << rect.width <<endl;
cout<< " x=" << rect.x <<endl;
cout<< " y=" << rect.y <<endl;</p>
<pre><code> }
</code></pre>
<p>} </p>
<p>cvShowImage(" object="" color="" range",="" frame2);="" }<="" p="">
void on_trackbar1(int position)
{
lowerH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar2(int position)
{
upperH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar3(int position)
{
lowerS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar4(int position)
{
upperS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar5(int position) { lowerV = position; cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh); cvShowImage("Object Detected", imgThresh); draw_conture(imgThresh); }
void on_trackbar6(int position) { upperV = position; cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh); cvShowImage("Object Detected", imgThresh); draw_conture(imgThresh); }
int main() {
CvCapture *capture = 0;
CvSeq* contours; CvMemStorage *storage = cvCreateMemStorage(0);
capture = cvCaptureFromCAM(0); if (!cvGrabFrame(capture)) { printf("ERROR"); }
IplImage *frame = cvQueryFrame(capture); // to store the retrieved frames from the camera or any video IplImage *resultImage = cvCreateImage(cvGetSize(frame), 8, 1); // to store the resulted image IplImage *thresholdedImage = cvCreateImage(cvGetSize(frame), 8, 1); // to store the thresholded image
cvNamedWindow("Object Color Range", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Object Detected", CV_WINDOW_AUTOSIZE);
while(1){
frame = cvQueryFrame(capture);
if (!frame) {
break;
}
cvCreateTrackbar("LowerH", "Object Detected", &lowerH, 256, on_trackbar1);
cvCreateTrackbar("UpperH", "Object Detected", &upperH, 256, on_trackbar2);
cvCreateTrackbar("LowerS", "Object Detected", &lowerS, 256, on_trackbar3);
cvCreateTrackbar("UpperS", "Object Detected", &upperS, 256, on_trackbar4);
cvCreateTrackbar("LowerV", "Object Detected", &lowerV, 256, on_trackbar5);
cvCreateTrackbar("UpperV", "Object Detected", &upperV, 256, on_trackbar6);
imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3 ) ;
imgThresh = cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvCvtColor(frame, imgHSV, CV_BGR2HSV);
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh); cvShowImage("Object Color Range", frame);
draw_conture(imgThresh); cvWaitKey(0);
cvReleaseImage(&imgHSV); cvReleaseImage(&imgThresh); cvReleaseImage(&frame);
cvDestroyAllWindows(); } return 0;
}
i am getting error like access violation reading location c++.
how could i solve this problem.
thank you very much in advance.
2 | No.2 Revision |
hi there,
i want to do detection using usb camera and the problem is that camera frame is not working fine means i can not get continue frame, i tried following code to detect the contour and draw rectangle around it.
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "stdio.h"
#include "string"
#include <stdlib.h>
using namespace cv;
using namespace std;
std;
int lowerH=168;
int upperH=256;
upperH=256;
int lowerS=175;
int upperS=256;
upperS=256;
int lowerV=0;
int upperV=256;
IplImage* frame = 0 ;
IplImage* frame2 = 0 ;
IplImage* imgHSV = 0 ;
IplImage* imgThresh = 0 ;
vector<vector<point> ;
vector<vector<Point> > contours;
vector<vec4i> hierarchy;
vector<Vec4i> hierarchy;
void draw_conture(IplImage * temp)
{
{
cv::Mat tempm(temp,true);
findContours( tempm , contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
vector<vector<cv::Point> > contours_poly( contours.size() );
vector<vector<cv::point> vector<rect> vector<Rect> boundRect( contours.size() );
vector<float>area( contours.size() );
);
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) ) ;
area[i]= contourArea(Mat(contours_poly[i]));
}
contourArea(Mat(contours_poly[i]));
}
const CvScalar color=CV_RGB(0x00,0xFF,0x00);
for( int i = 0; i< contours.size(); i++ )
{
if (area[i]>1000)
rectangle(tempm, boundRect[i].tl(), boundRect[i].br(), color, 1, 1, 0 );
}
}
CvSeq* contour; //hold the pointer to a contour
CvSeq* result; //hold sequence of points of a contour
CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
contours
cvFindContours(temp, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
cvPoint(0,0));
for (; contour != 0; contour = contour->h_next)
{
result = cvApproxPoly(contour, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);
CvRect rect = cvBoundingRect(contour, 0); //extract bounding box for current contour
if( cvContourArea(result, CV_WHOLE_SEQ,0) >= 1000 )
{
cvDrawContours(frame, contour, CV_RGB(0,0,255), CV_RGB(0,0,255), 2, 1, 8, cvPoint(0, 0));
frame2 = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
cvCopy(frame, frame2);
cvRectangle(frame2, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(0,0,255),2, 8, 0);
cout<< "height =" << rect.height <<endl; cout<<="" "width=" << <<endl;
cout<< "width =" << rect.width <<endl;
cout<< " x=" << <<endl;
cout<< "x =" << rect.x <<endl;
cout<< " y=" << <<endl;
cout<< "y =" << rect.y <<endl;</p>
<pre><code> <<endl;
}
}
cvShowImage("Object Color Range", frame2);
}
</code></pre>
<p>} </p>
<p>cvShowImage(" object="" color="" range",="" frame2);="" }<="" p="">
trackbar
void on_trackbar1(int position)
{
lowerH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
}
void on_trackbar2(int position)
{
upperH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
}
void on_trackbar3(int position)
{
lowerS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
}
void on_trackbar4(int position)
{
upperS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
}
void on_trackbar5(int position)
{
lowerV = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
}
void on_trackbar6(int position)
{
upperV = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
trackbar
int main()
{
{
CvCapture *capture = 0;
0;
CvSeq* contours;
CvMemStorage *storage = cvCreateMemStorage(0);
cvCreateMemStorage(0);
capture = cvCaptureFromCAM(0);
if (!cvGrabFrame(capture)) {
printf("ERROR");
}
}
IplImage *frame = cvQueryFrame(capture); // to store the retrieved frames from the camera or any video
IplImage *resultImage = cvCreateImage(cvGetSize(frame), 8, 1); // to store the resulted image
IplImage *thresholdedImage = cvCreateImage(cvGetSize(frame), 8, 1); // to store the thresholded image
image
cvNamedWindow("Object Color Range", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Object Detected", CV_WINDOW_AUTOSIZE);
while(1){
frame = cvQueryFrame(capture);
if (!frame) {
break;
}
cvCreateTrackbar("LowerH", "Object Detected", &lowerH, 256, on_trackbar1);
cvCreateTrackbar("UpperH", "Object Detected", &upperH, 256, on_trackbar2);
cvCreateTrackbar("LowerS", "Object Detected", &lowerS, 256, on_trackbar3);
cvCreateTrackbar("UpperS", "Object Detected", &upperS, 256, on_trackbar4);
cvCreateTrackbar("LowerV", "Object Detected", &lowerV, 256, on_trackbar5);
cvCreateTrackbar("UpperV", "Object Detected", &upperV, 256, on_trackbar6);
imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3 ) ;
imgThresh = cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvCvtColor(frame, imgHSV, CV_BGR2HSV);
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
cvShowImage("Object Color Range", frame);
frame);
draw_conture(imgThresh);
cvWaitKey(0);
cvWaitKey(0);
cvReleaseImage(&imgHSV);
cvReleaseImage(&imgThresh);
cvReleaseImage(&frame);
cvReleaseImage(&frame);
cvDestroyAllWindows();
}
return 0;
}
0; }i am getting error like access violation reading location c++.
how could i solve this problem.
thank you very much in advance.