I have a code which basically detects playing cards, isolates them from a dynamic background based on HSV settings, then uses contours to detect the 4 points of the card to find the exact x and y position of the card. From there, the ROI is set and I can perform further processing to detect the face value of the card.
However, I the code seems to be breaking and I can't seem to find the root cause of it.
I have cleared images & memory storages, I've ensured that all the Iplimages have the same formatting and resolution.
IplImage* GetThresholdedImage(IplImage* imgHSV){
IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvInRangeS(imgHSV, cvScalar(95,67,170), cvScalar(110,119,254), imgThresh); //Morning
return imgThresh;
}
IplImage* RedCheck(IplImage* imgBGR){
IplImage* img=cvCreateImage(cvGetSize(imgBGR),IPL_DEPTH_8U, 1);
cvInRangeS(imgBGR, cvScalar(0,0,100), cvScalar(100,100,254), img); //BGR
return img;
}
int main()
{
CvCapture* capture =0;
capture = cvCaptureFromCAM(0);
if(!capture)
{
printf("Capture failure\n");
return -1;
}
IplImage* frame = cvCreateImage(cvSize(48,64),IPL_DEPTH_8U,3);
while(true)
{
frame = cvQueryFrame(capture);
if(!frame) break;
frame=cvCloneImage(frame);
cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel
IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV
IplImage* imgThresh = GetThresholdedImage(imgHSV);
cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel
CvSeq* contours;
CvSeq* result;
CvMemStorage *storage = cvCreateMemStorage(0);
cvFindContours(imgThresh, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
while(contours)
{
result = cvApproxPoly(contours, sizeof(CvContour),storage,CV_POLY_APPROX_DP,cvContourPerimeter(contours)*0.02,0);
if(result->total == 4)
{
CvPoint *pt[4];
for(int i=0;i<4;i++)
{
pt[i] = (CvPoint*)cvGetSeqElem(result,i);
}
if (cvArcLength(result,CV_WHOLE_SEQ,1) >= 400)
{
cvLine(imgThresh,*pt[0],*pt[1],cvScalar(255,0,0),4);
cvLine(imgThresh,*pt[1],*pt[2],cvScalar(255,0,0),4);
cvLine(imgThresh,*pt[2],*pt[3],cvScalar(255,0,0),4);
cvLine(imgThresh,*pt[3],*pt[0],cvScalar(255,0,0),4);
int ROIwidth = abs((*pt[0]).x - (*pt[1]).x);
int ROIheight = abs((*pt[1]).y - (*pt[2]).y);
cvSetImageROI(frame,cvRect((*pt[1]).x,(*pt[1]).y,ROIwidth,ROIheight));
IplImage* temp = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
cvCopy(frame,temp,0);
cvResetImageROI(frame);
cvNamedWindow( "ROI", CV_WINDOW_AUTOSIZE );
cvShowImage( "ROI", temp);
printf("Width = %d\n",ROIwidth); //255-275
printf("Height = %d\n",ROIheight); //140-160
//Card Value Detection Starts Here
IplImage* colorcheck = RedCheck(temp);
int redpixelcheck = cvCountNonZero(colorcheck);
if (redpixelcheck <= 15)
{
printf("Card is Black\n");
}
else if (redpixelcheck >= 16)
{
printf("Card is Red\n");
}
//Card Value Detection Ends Here
cvReleaseImage(&temp);
cvReleaseImage(&frame);
cvReleaseImage(&colorcheck);
delete &ROIwidth;
delete &ROIheight;
delete &redpixelcheck;
}
//delete [] pt[4];
}
delete &result;
contours = contours->h_next;
//cvPutText (frame_t,text,cvPoint(200,400), &font, cvScalar(255,255,0));
}
cvNamedWindow( "Contour", CV_WINDOW_AUTOSIZE );
cvShowImage( "Contour", imgThresh);
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
cvShowImage("Video", frame);
//Clean up used images
cvReleaseImage(&imgHSV);
cvReleaseImage(&imgThresh);
cvReleaseImage(&frame);
cvClearMemStorage(storage);
cvReleaseMemStorage(&storage);
//Wait 50mS
int c = cvWaitKey(10);
//If 'ESC' is pressed, break the loop
if((char)c==27 ) break;
}
cvDestroyAllWindows() ;
cvReleaseCapture(&capture);
return 0;
}
The disassembly always points to the same address
770915DE add esp,4
Related
i am trying to learn about OpenCV and found a really good tutorial on youtube however, everytime i am told to "select a video device" on Visual Studio, a R6010 error pops up and i have to keep aborting the program. Here is the source code. I am using OpenCV 2.2, 2010 Visua Studio and the camera in question is a HP TrueVision HD. Thanks!
//tracker1
#include<opencv\cvaux.h>
#include<opencv\highgui.h>
#include<opencv\cxcore.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char* []){
CvSize size640x480 = cvSize(640, 480);
CvCapture* p_capWebCam; //assign a webcam (later)
IplImage* p_imgOriginal; //image given by the webcam
IplImage* p_imgProcessed; //webcam image processed
CvMemStorage* p_strStorage; //passing stored variables
CvSeq* p_seqCircles;
float* p_fltXYRadius; //3 points, 0 = X, 1 = Y, 2 = Radius
int i; //looping integer
char charCheckForEscKey;
p_capWebCam = cvCaptureFromCAM(0);
if(p_capWebCam == NULL){
printf("error, webcam not found");
getchar();
return(-1);
}
cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
cvNamedWindow("processed", CV_WINDOW_AUTOSIZE);
p_imgProcessed = cvCreateImage(size640x480, IPL_DEPTH_8U, 1);
while(1){
p_imgOriginal = cvQueryFrame(p_capWebCam); //get frame of webcame
if(p_capWebCam == NULL){ //when the frame is not attained
printf("error, no frames were attained");
getchar();
break;
}
cvInRangeS(p_imgOriginal, CV_RGB(175,0,0), CV_RGB(256, 100, 100), p_imgProcessed);
p_strStorage = cvCreateMemStorage(0);
cvSmooth(p_imgProcessed, p_imgProcessed, CV_GAUSSIAN, 9 ,9);
p_seqCircles = cvHoughCircles(p_imgProcessed, p_strStorage, CV_HOUGH_GRADIENT, 2, p_imgProcessed->height/4, 100, 50, 10, 400);
for(i=0; i<p_seqCircles->total; i++){
p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i);
printf("ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0], p_fltXYRadius[1], p_fltXYRadius[2]);
cvCircle(p_imgOriginal, cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), 3, CV_RGB(0, 255, 0), CV_FILLED);
cvCircle(p_imgOriginal, cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), cvRound(p_fltXYRadius[2]), CV_RGB(255, 0, 0), 3);
}
cvShowImage("original", p_imgOriginal);
cvShowImage("processed", p_imgProcessed);
cvReleaseMemStorage(&p_strStorage);
charCheckForEscKey = cvWaitKey(10);
if(charCheckForEscKey == 27) break;
}
cvReleaseCapture(&p_capWebCam);
cvDestroyWindow("original");
cvDestroyWindow("processed");
}
I am trying to manipulate pixel using sdl and manage to read them up now. Below is my sample code. When I print I this printf("\npixelvalue is is : %d",MyPixel); I get values like this
11275780
11275776
etc
I know these are not in hex form but how to manipulate say I want to filter just the blue colors out? Secondly after manipulation how to generate the new image?
#include "SDL.h"
int main( int argc, char* argv[] )
{
SDL_Surface *screen, *image;
SDL_Event event;
Uint8 *keys;
int done = 0;
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
printf("Can't init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_WM_SetCaption("sample1", "app.ico");
/* obtain the SDL surfance of the video card */
screen = SDL_SetVideoMode(640, 480, 24, SDL_HWSURFACE);
if (screen == NULL)
{
printf("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
printf("Loading here");
/* load BMP file */
image = SDL_LoadBMP("testa.bmp");
Uint32* pixels = (Uint32*)image->pixels;
int width = image->w;
int height = image->h;
printf("Widts is : %d",image->w);
for(int iH = 1; iH<=height; iH++)
for(int iW = 1; iW<=width; iW++)
{
printf("\nIh is : %d",iH);
printf("\nIw is : %d",iW);
Uint32* MyPixel = pixels + ( (iH-1) + image->w ) + iW;
printf("\npixelvalue is is : %d",MyPixel);
}
if (image == NULL) {
printf("Can't load image of tux: %s\n", SDL_GetError());
exit(1);
}
/* Blit image to the video surface */
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
/* free the image if it is no longer needed */
SDL_FreeSurface(image);
/* process the keyboard event */
while (!done)
{
// Poll input queue, run keyboard loop
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
{
done = 1;
break;
}
}
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_q])
{
done = 1;
}
// Release CPU for others
SDL_Delay(100);
}
// Release memeory and Quit SDL
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}
Use SDL_MapRGB and SDL_MapRGBA to sort colors out. SDL will filter it out for you, based on surface format.
Just like this:
Uint32 rawpixel = getpixel(surface, x, y);
Uint8 red, green, blue;
SDL_GetRGB(rawpixel, surface->format, &red, &green, &blue);
You are printing the value of the pointer MyPixel. To get the value you have to dereference the pointer to the pixel value like this: *MyPixel
Then the printf would look like this:
printf("\npixelvalue is : %d and the address of that pixel is: %p\n",*MyPixel , MyPixel);
Other errors:
Your for loops are incorrect. You should loop from 0 to less than width or height, or else you will read uninitialized memory.
You didn't lock the surface. Although you are only reading the pixels and nothing should go wrong it is still not correct.
Test for correctness if the image pointer comes after you are already using the pointer. Put the test right after the initialization.
If I recall correctly I used sdl_gfx for pixel manipulation.
It also contains function like drawing a circle, oval etc.
I am developing a program in VS 2010 using OpenCV. I want to measure the luminance of every frame that the computer's camera captures. However, the camera's software stabilizes the luminance after 2-3 frames. Eg, if i put my thumb in front of the camera the first frame's luminance is 2 (scale from 0 to 255), but then while keeping my thumb in front of the camera the luminance becomes 7 and the 20 - it is stabilized there for the next frames. So the camera tries to make too dark pictures brighter and too bright pictures darker.
How can i measure the actual luminance without the camera's interference?
My code is:
#ifdef _CH_
#pragma package <opencv>
#endif
#include "stdafx.h"
#include <highgui.h>
#include "cv.h"
#include <stdio.h>
#include <stdlib.h>
#include "..\utilities.h"
int _tmain(int argc, _TCHAR* argv[])
{
FILE *file;
IplImage *img;
IplImage* grayscale_image;
int c, i, j, Luminance = 0, Pixel_Num = 0;
int Avg_Luminance;
int width_step;
int pixel_step;
// allocate memory for an image
// capture from video device #1
CvCapture* capture;
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
if(file = fopen("luminance_value.txt", "w+"))
{
// retrieve the captured frame
capture= cvCaptureFromCAM(1);
img=cvQueryFrame(capture);
grayscale_image = cvCreateImage( cvGetSize(img), 8, 1 );
cvConvertImage( img, grayscale_image );
width_step= grayscale_image->widthStep;
pixel_step= grayscale_image->widthStep/grayscale_image->width;
Pixel_Num = grayscale_image->width * grayscale_image->height;
for(i = 0; i < grayscale_image->height; i++)
{
for(j = 0; j < grayscale_image->width; j++)
{
unsigned char* point = GETPIXELPTRMACRO( grayscale_image, j, i, width_step, pixel_step);
Luminance += point[0];
}
}
Avg_Luminance = Luminance / Pixel_Num;
//Avg_Luminance = cvGetCaptureProperty(capture,CV_CAP_PROP_BRIGHTNESS);
//file = fopen("luminance_value.txt", "w+");
fprintf(file, "%d", Avg_Luminance);
fclose(file);
printf("Avg_Luminance = %d\n", Avg_Luminance);
Luminance = 0;
Pixel_Num = 0;
// show the image in the window
cvShowImage("mainWin", grayscale_image );
cvReleaseCapture(&capture);
// wait 10 ms for a key to be pressed
c=cvWaitKey(10000);
// escape key terminates program
if(c == 27)
break;
}
else
{
continue;
}
}
return 0;
}
i'm trying to make a checkers game and atm i'm doing the interface with SDL, but i'm just learning C and SDL, how can I move a surface I added to the screen ? I want it the simplest as possible, just remove from X and show on Y, how do I remove a surface to make it appear on another place on the screen ? here is my code:
#include "SDL.h"
#define BRANCA 2
#define PRETA 1
#define DAMA 2
#define NORMAL 1
//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces that will be used
SDL_Surface *pecaPreta = NULL;
SDL_Surface *pecaBranca = NULL;
SDL_Surface *pecaDamaPreta = NULL;
SDL_Surface *pecaDamaBranca = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *load_image(char * filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename);
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}
void inserePeca(int tipo, int posX, int posY, int cor)
{
switch(cor)
{
case 1:
switch (tipo)
{
case 1:
apply_surface(posX, posY, pecaPreta, screen);
break;
case 2:
apply_surface(posX, posY, pecaDamaPreta, screen);
break;
}
break;
case 2:
switch (tipo)
{
case 1:
apply_surface(posX, posY, pecaBranca, screen);
break;
case 2:
apply_surface(posX, posY, pecaDamaBranca, screen);
break;
}
break;
}
}
int main()
{
int quit = 0;
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Jogo de Damas 0.1b", NULL );
//Load the images
pecaPreta = load_image( "pecapreta.bmp" );
pecaBranca = load_image("pecabranca.bmp");
pecaDamaPreta = load_image("pecadamapreta.bmp");
pecaDamaBranca = load_image("pecadamabranca.bmp");
background = load_image( "tabuleiro.bmp" );
//Apply the background to the screen
apply_surface( 0, 0, background, screen );
inserePeca(DAMA, 0,0, BRANCA);
inserePeca(NORMAL, 80,0, PRETA);
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
while( quit == 0 )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = -1;
}
}
}
//Free the surfaces
SDL_FreeSurface( pecaPreta );
SDL_FreeSurface( background );
//Quit SDL
SDL_Quit();
return 0;
}
as you can see I add a block on "inserePeca", I want to move it after I create it
The buffer for the screen doesn't keep all the things you draw on it as separate items -- it just holds the end result of all the drawing operations. So, you can't just draw the background, then draw a piece on it, then move the piece around -- you need to redraw the affected parts of the screen with the required changes.
You still have the images of the pieces, and you still have the background image; the way to move a piece you've drawn is simply to restore the background to the old position by blitting it again, and then blit the piece in the new position. Rather than drawing the whole screen and all the pieces over again, though, you can just draw the changed areas: blit just a part of the background to erase the old square, and then blit the piece onto the new square.
The following function is similar to your apply_surface() function, but instead of copying the whole source image to the the given coordinates of the destination, it copies a region of a given width and height from the given coordinates of the source image to the same coordinates of the destination. This can then be used to restore the background for a small part of the screen.
/* Blit a region from src to the corresponding region in dest. Uses the same
* x and y coordinates for the regions in both src and dest. w and h give the
* width and height of the region, respectively.
*/
void erase_rect( int x, int y, int w, int h, SDL_Surface *src, SDL_Surface *dest)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
offset.w = w;
offset.h = h;
SDL_BlitSurface( src, &offset, dest, &offset );
}
So if your squares are 50x50, and you need to move a piece from a square at (120, 40) to the square at (170, 90), you could do something like the following:
/* erase old 50x50 square at (120,40) (to background image) */
erase_rect( 120, 40, 50, 50, background, screen );
/* draw piece at new position of (170,90) */
inserePeca(NORMAL, 170, 90, PRETA);
I'm using opencv sequences to store points resulted from cvhoughline2.
I create the sequence and make some operations on it, but when I run the program it breaks in the line cvseqremove(seq,index) and gives me the exception:
exception at memory location
When I put the cursor on the seq in cvseqremove(seq,index), it writes to me: h_prev=0*000000000000000.
I wrote that code, i'll appreciate any help
void APPROXIMATE_LINES(IplImage* image,unsigned int xsize,unsigned int ysize,double width)
{
unsigned int i;
IplImage* color_dst = cvCreateImage(cvGetSize(image), 8,3);
IplImage* dst = cvCreateImage(cvGetSize(image), 8,1);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* Filtered_Lines=0;
boolean isbreak;
double distance=4.0;
CvPoint Mid_Point;
CvPoint First_Mid_Point;
CvSeq* lines = cvCreateSeq(CV_SEQ_ELTYPE_POINT,sizeof(CvSeq),sizeof(CvPoint),storage);
cvCanny(image,dst,180,250,3);
cvCvtColor(dst,color_dst,CV_GRAY2BGR);
lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 4, CV_PI/165, 95, 93, 75);
while (lines->total>1)
{
CvPoint* First_Line = (CvPoint*)cvGetSeqElem(lines,0);
First_Mid_Point=mid(First_Line[0],First_Line[1]);
isbreak=FALSE;
for( i =1; i < lines->total; i++ )
{
CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
Mid_Point=mid(line[0],line[1]);
if(dist(First_Mid_Point.x,First_Mid_Point.y,Mid_Point.x,Mid_Point.y)<distance)
{
cvSeqRemove(lines,i);
isbreak=TRUE;
break;
}
} /*End_for*/
if(!isbreak)
{
cvSeqPushFront(Filtered_Lines,First_Line); // <--- breaks here
cvSeqRemove(lines,0);
}
} /*End_while*/
for(i=0;i<Filterd_Lines;i++)
{
CvPoint* Filtered = (CvPoint*)cvGetSeqElem(Filterd_Lines,i);
cvLine(color_dst, Filtered[0], Filtered[1], cvScalar(0,0,255,0),1,8,0);
}
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1",color_dst);
cvWaitKey(0);
cvReleaseImage( &color_dst);
cvDestroyWindow( "Example1" );
}
The function signature is:
void cvSeqRemove(CvSeq* seq, int index)
But on one part of the code you are doing:
cvSeqRemove(lines,&i);
Which means you are passing the memory address of the variable i instead of it's value, and this is not what you want to do.
There might be other bugs.