The Learning of OpenCV
Here contains just the exercises in book "Learning OpenCV" and of course I believe there is better solution for them.
problems:
1. If you set cvSetImageCOI, don't forget to set it back with parameter "0", otherwise you may end up with single channel.
2. If you get your IplImage from "cvCapture", then do NOT release it as it is not created by you!
3. There is some memory allocation in "cvSubImageHeader, and do NOT forget to release them.
4. I tried to erase my drawing lines by "xor" it again, but it seems not working by using "cvGetRow". Maybe it is because I mess up with "cvSetImageROI without reset it. So, I just use cvCopy to
overwrite it.
5. When you draw your rectangle of some width of line, be careful that the rectangle is bigger than that width of line.
6. The color sequence in bytes order is "BGR".
The following are exercises in chapter 4.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
CvFont myfont = cvFont(1, 2);
char* szCombineName = "combine image";
bool bMouseButtonDown = false;
int x = 0, y = 0;
void doDrawing(IplImage* pImage)
{
char buffer[256];
int winX = 0, winY = 0;
if (bMouseButtonDown)
{
winX = x;
winY = y;
sprintf(buffer, "mouse click at [%d,%d]", x, y);
cvPutText(pImage, buffer, cvPoint(winX, winY), &myfont, cvScalar(255,0,0));
}
}
void myMouseCallback(int event, int myx, int myy, int flag, void* pUser)
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
bMouseButtonDown = true;
x = myx;
y = myy;
break;
case CV_EVENT_LBUTTONUP:
bMouseButtonDown = false;
break;
}
}
void exercise1()
{
char* szVideoName = "e:\\mytest.avi";
char* szWindowName = "my video";
char* szGrayName = "gray image";
char* szCannyName = "canny image";
CvCapture* pCapture = NULL;
IplImage* pImage = NULL;
IplImage* pGrayImg = NULL;
IplImage* pCannyImg = NULL;
IplImage* pCombineImg = NULL;
IplImage* pSub1= NULL;
IplImage* pSub2= NULL;
IplImage* pSub3= NULL;
int nWidth = 0, nHeight = 0;
cvNamedWindow(szWindowName);
cvNamedWindow(szGrayName);
cvNamedWindow(szCannyName);
cvNamedWindow(szCombineName);
pCapture = cvCreateFileCapture(szVideoName);
if (pCapture)
{
nWidth = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH);
nHeight = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT);
pGrayImg = cvCreateImage(cvSize(nWidth, nHeight), 8, 1);
pCannyImg = cvCreateImage(cvSize(nWidth, nHeight), 8, 1);
pCombineImg = cvCreateImage(cvSize(nWidth, 3*nHeight), 8, 1);
pSub1 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
pSub2 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
pSub3 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
pSub1->origin = pCombineImg->origin;
pSub2->origin = pCombineImg->origin;
pSub3->origin = pCombineImg->origin;
pSub1->widthStep = pCombineImg->widthStep;
pSub2->widthStep = pCombineImg->widthStep;
pSub3->widthStep = pCombineImg->widthStep;
pSub1->imageData = pCombineImg->imageData;
pSub2->imageData = pCombineImg->imageData + nHeight*pCombineImg->widthStep;
pSub3->imageData = pCombineImg->imageData + nHeight*2*pCombineImg->widthStep;
cvSetMouseCallback(szCombineName, myMouseCallback, pCombineImg);
//cvResizeWindow(szCombineName, pCombineImg->width, pCombineImg->height);
do
{
if (cvWaitKey(15) == 27)
{
break;
}
pImage = cvQueryFrame(pCapture);
if (pImage)
{
cvConvertImage(pImage, pGrayImg, 0);
cvCanny(pGrayImg, pCannyImg, 1, 2, 5);
//cvShowImage(szWindowName, pImage);
//cvShowImage(szGrayName, pGrayImg);
//cvShowImage(szCannyName, pCannyImg);
cvSetImageCOI(pImage, 1);
cvCopy(pImage, pSub1, NULL);
cvCopy(pGrayImg, pSub2, NULL);
cvCopy(pCannyImg, pSub3, NULL);
cvPutText(pSub1, szWindowName, cvPoint(nWidth/2, nHeight/2), &myfont, cvScalar(255,0,0));
cvPutText(pSub2, szGrayName, cvPoint(nWidth/2, nHeight/2), &myfont, cvScalar(255,0,0));
cvPutText(pSub3, szCannyName, cvPoint(nWidth/2, nHeight/2), &myfont, cvScalar(255,0,0));
doDrawing(pCombineImg);
cvShowImage(szCombineName, pCombineImg);
cvSetImageCOI(pImage, 0);
}
}
while (true);
// do NOT release pImage because it is only retrieved from capture
//cvReleaseImage(&pImage);
cvReleaseImageHeader(&pSub1);
cvReleaseImageHeader(&pSub2);
cvReleaseImageHeader(&pSub3);
cvReleaseImage(&pGrayImg);
cvReleaseImage(&pCannyImg);
cvReleaseImage(&pCombineImg);
cvReleaseCapture(&pCapture);
}
cvDestroyAllWindows();
}
void myMouseOnClick(int event, int x, int y, int flag, void* pUser)
{
char buffer[256];
CvFont myFont = cvFont(2, 2);
IplImage* pImage = (IplImage*)pUser;
char* ptr = NULL;
unsigned char r=0, g=0, b=0;
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
ptr = pImage->imageData + y*pImage->widthStep + x * pImage->depth* pImage->nChannels/8;
r = ptr[0];
g = ptr[1];
b = ptr[2];
sprintf(buffer, "rgb [%d,%d,%d]", r,g,b);
cvPutText(pImage, buffer, cvPoint(x, y), &myFont, cvScalar(255,0,0));
break;
}
}
void exercise2()
{
IplImage* pImage = NULL; //, *pCloneImage = NULL;
pImage = cvLoadImage("mytest.jpg");
//pCloneImage = cvCloneImage(pImage);
cvNamedWindow("mytest");
cvSetMouseCallback("mytest", myMouseOnClick, pImage);
do
{
cvShowImage("mytest", pImage);
if (cvWaitKey(1000)==27)
{
break;
}
}
while (true);
cvReleaseImage(&pImage);
cvDestroyWindow("mytest");
}
#define HistogramWidth 48
#define HistogramHeight 800
IplImage* pImage = NULL, *pCloneImage = NULL, *pHistImage= NULL;
CvRect rect = cvRect(0,0,0,0);
bool bStart = false;
#if 0
void eraseFrame()
{
CvMat vecSrc, vecDst;
cvSetImageROI(pImage, rect);
cvSetImageROI(pCloneImage, rect);
cvGetRow(pImage, &vecSrc, 0);
cvGetRow(pCloneImage, &vecDst, 0);
cvCopy(&vecSrc, &vecDst);
cvGetRow(pImage, &vecSrc, rect.height-1);
cvGetRow(pCloneImage, &vecDst, rect.height-1);
cvCopy(&vecSrc, &vecDst);
cvGetCol(pImage, &vecSrc, 0);
cvGetCol(pCloneImage, &vecDst, 0);
cvCopy(&vecSrc, &vecDst);
cvGetCol(pImage, &vecSrc, rect.width-1);
cvGetCol(pCloneImage, &vecDst, rect.width-1);
cvCopy(&vecSrc, &vecDst);
cvResetImageROI(pImage);
cvResetImageROI(pCloneImage);
}
#else
void eraseFrame()
{
cvSetImageROI(pImage, rect);
cvSetImageROI(pCloneImage, rect);
cvCopy(pCloneImage, pImage);
cvResetImageROI(pImage);
cvResetImageROI(pCloneImage);
}
#endif
#define LINE_WIDTH 1
void drawFrame()
{
cvRectangle(pImage, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width-1, rect.y+rect.height-1), cvScalarAll(255), LINE_WIDTH);
//cvLine(pImage, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), cvScalarAll(255), LINE_WIDTH);
}
void drawHistogram()
{
char* ptr = NULL, *pRow = NULL, *pCol = NULL;
char buffer[32];
int hist[3][8]= {0};
int index = 0;
int nPixWidth = 0;
int r, c, i, nOffset = 0;
int x = 0 , y = 0;
CvFont myFont = cvFont(1,1);
int nMax = 0, nScale=1;
nPixWidth = pCloneImage->nChannels*pCloneImage->depth/8;
ptr = pCloneImage->imageData + rect.y*pCloneImage->widthStep + rect.x*nPixWidth;
pRow = ptr;
for (r = 0; r < rect.height; r ++)
{
pCol = pRow;
for (c = 0; c < rect.width; c ++)
{
for (i =0; i < 3; i ++)
{
index = pCol[i] / 32;
hist[i][index] ++;
if (hist[i][index] > nMax)
{
nMax = hist[i][index];
}
}
pCol += nPixWidth;
}
pRow += pCloneImage->widthStep;
}
cvSet(pHistImage, cvScalarAll(255));
y = HistogramHeight;
nScale = HistogramHeight / HistogramHeight;
if (nScale == 0)
{
nScale = 1;
}
for (c = 0; c < 8; c ++)
{
for (i = 0; i < 3; i ++)
{
unsigned char color[3]={0};
x = nOffset;
color[i] = 255;
if (hist[i][c] / nScale != 0)
{
cvSetImageROI(pHistImage, cvRect(x+i*HistogramWidth, y - hist[i][c]/nScale, HistogramWidth, hist[i][c]/nScale));
cvSet(pHistImage, cvScalar(color[0], color[1], color[2]));
cvResetImageROI(pHistImage);
}
sprintf(buffer, "%d", hist[i][c]);
cvPutText(pHistImage, buffer, cvPoint(x+i*HistogramWidth, y-100), &myFont, cvScalarAll(0));
}
nOffset += HistogramWidth*3;
}
cvShowImage("histogram", pHistImage);
}
void myMouseCallback3(int event, int x, int y, int flag, void* pUser)
{
int w = 0, h = 0;
switch (event)
{
case CV_EVENT_MOUSEMOVE:
if (!bStart)
{
break;
}
w = x - rect.x;
h = y - rect.y;
if (w > LINE_WIDTH && h > LINE_WIDTH)
{
if (w != rect.width || h != rect.height)
{
if (rect.width > 0 && rect.height > 0)
{
eraseFrame();
/////////////////////////////////
}
rect.width = w;
rect.height = h;
//cvSetImageROI(pImage, rect);
//cvSetImageROI(pCloneImage, rect);
//cvSet(pImage, cvScalarAll(255));
drawFrame();
//cvResetImageROI(pImage);
//cvResetImageROI(pCloneImage);
}
}
break;
case CV_EVENT_LBUTTONDOWN:
if (rect.width > 0 && rect.height > 0)
{
cvSetImageROI(pImage, rect);
cvSetImageROI(pCloneImage, rect);
cvCopy(pCloneImage, pImage);
cvResetImageROI(pImage);
cvResetImageROI(pCloneImage);
/////////////////////////////////
}
rect.x = x;
rect.y = y;
rect.width = rect.height = 0;
bStart = true;
break;
case CV_EVENT_LBUTTONUP:
if (rect.width > 0 && rect.height > 0)
{
cvSetImageROI(pImage, rect);
cvSet(pImage, cvScalarAll(255));
cvResetImageROI(pImage);
drawHistogram();
/////////////////////////////////
}
bStart = false;
break;
}
}
void exercise3()
{
pImage = cvLoadImage("mytest.jpg");
pCloneImage = cvCloneImage(pImage);
cvNamedWindow("histogram");
cvNamedWindow("mytest");
cvSetMouseCallback("mytest", myMouseCallback3, NULL);
pHistImage = cvCreateImage(cvSize(8 * HistogramWidth * 3, HistogramHeight+200), 8, 3);
do
{
cvShowImage("mytest", pImage);
if (cvWaitKey(100)==27)
{
break;
}
}
while (true);
cvReleaseImage(&pImage);
cvReleaseImage(&pCloneImage);
cvDestroyWindow("mytest");
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
exercise3();
//myTest();
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
There is a problem that I don't know how to control the program flow. For example, I setup two cvWaitKey because I place one in my main program and another
one in the while loop of "playing video". You see, if you don't place a "cvWaitKey" inside the video playing loop, then there is simply no redrawing of
window. Just image there is no overlapping threads to either decode video or draw window.
The following is a simple program acting as an video player(exercise4)
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
int g_nProgress = 0;
int g_nPause = 0;
int g_nWait = 0;
CvCapture* pCapture = NULL;
int nValue = -1;
char* szWindowName = "myplayer";
void switchCallback4(int nPos)
{
double fPos = 0.0;
fPos = (double)nPos / (double)10;
if (pCapture)
{
cvSetCaptureProperty(pCapture, CV_CAP_PROP_POS_AVI_RATIO, fPos);
}
}
void myPlayer()
{
IplImage* pImage = NULL;
do
{
if (g_nPause == 0)
{
break;
}
pImage = cvQueryFrame(pCapture);
if (pImage)
{
cvShowImage(szWindowName, pImage);
}
nValue = cvWaitKey(50);
}
while (nValue == -1);
}
void switchCallback5(int nPos)
{
if (nPos == 1)
{
myPlayer();
}
}
void exercise4()
{
pCapture = cvCreateFileCapture("mytest.avi");
if (pCapture)
{
cvNamedWindow(szWindowName);
cvCreateTrackbar("progress", szWindowName, &g_nProgress, 10, switchCallback4);
cvCreateTrackbar("pause", szWindowName, &g_nPause, 1, switchCallback5);
do
{
if (cvWaitKey(0) == 27)
{
break;
}
}
while(true);
cvReleaseCapture(&pCapture);
cvDestroyWindow(szWindowName);
}
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
exercise4();
return 0;
}
chapter 5 exercises:
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void exercise1()
{
char* szWindowName[] = {"loadImage", "simpleImage", "simpleNoScale", "NoMedian", "Guassian", "bilateral"};
IplImage* pImage = NULL, *pSimpleImg = NULL, *pNoScaleImg = NULL, *pMedianImg = NULL, *pGaussianImg = NULL, *pBilateralImg = NULL;
int i = 0;
for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i ++)
{
cvNamedWindow(szWindowName[i]);
}
pImage = cvLoadImage("mytest.jpg");
if (pImage)
{
pSimpleImg = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pNoScaleImg = cvCreateImage(cvSize(pImage->width, pImage->height), IPL_DEPTH_16S, pImage->nChannels);
pMedianImg = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pGaussianImg = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pBilateralImg = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
cvSmooth(pImage, pSimpleImg, CV_BLUR, 5,5, 5,5);
cvSmooth(pImage, pNoScaleImg, CV_BLUR_NO_SCALE,11,11, 32,0.5);
cvSmooth(pImage, pMedianImg, CV_MEDIAN,5,5, 5,5);
cvSmooth(pImage, pGaussianImg, CV_GAUSSIAN,5,5, 5,5);
cvSmooth(pImage, pBilateralImg, CV_BILATERAL, 3,3,0.01, 0.003);
cvShowImage(szWindowName[0], pImage);
cvShowImage(szWindowName[1], pSimpleImg);
cvShowImage(szWindowName[2], pNoScaleImg);
cvShowImage(szWindowName[3], pMedianImg);
cvShowImage(szWindowName[4], pGaussianImg);
cvShowImage(szWindowName[5], pBilateralImg);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pSimpleImg);
cvReleaseImage(&pNoScaleImg);
cvReleaseImage(&pMedianImg);
cvReleaseImage(&pGaussianImg);
cvReleaseImage(&pBilateralImg);
}
cvDestroyAllWindows();
}
void exercise2()
{
char* szWindowName[] = {"original", "3x3", "5x5", "5x5 second", "9x9", "11x11"};
IplImage* pImage = NULL, *pImage3 = NULL, *pImage5 = NULL, *pImage5_2 = NULL, *pImage7 = NULL, *pImage9 = NULL, *pImage11 = NULL;
int i = 0;
for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i ++)
{
cvNamedWindow(szWindowName[i]);
}
pImage = cvLoadImage("mytest.jpg");
if (pImage)
{
pImage3 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pImage5 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pImage5_2 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pImage7 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pImage9 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
pImage11 = cvCreateImage(cvSize(pImage->width, pImage->height), pImage->depth, pImage->nChannels);
cvSmooth(pImage, pImage3, CV_GAUSSIAN, 3,3);
cvSmooth(pImage, pImage5, CV_GAUSSIAN, 5,5);
cvSmooth(pImage5, pImage5_2, CV_GAUSSIAN, 5,5);
cvSmooth(pImage, pImage7, CV_GAUSSIAN,7,7);
cvSmooth(pImage, pImage9, CV_GAUSSIAN,9,9);
cvSmooth(pImage, pImage11, CV_GAUSSIAN,11,11);
cvShowImage(szWindowName[0], pImage);
cvShowImage(szWindowName[1], pImage3);
cvShowImage(szWindowName[2], pImage5);
cvShowImage(szWindowName[3], pImage5_2);
cvShowImage(szWindowName[4], pImage7);
cvShowImage(szWindowName[5], pImage9);
cvShowImage(szWindowName[6], pImage11);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pImage3);
cvReleaseImage(&pImage5);
cvReleaseImage(&pImage5_2);
cvReleaseImage(&pImage7);
cvReleaseImage(&pImage9);
cvReleaseImage(&pImage11);
}
cvDestroyAllWindows();
}
void exercise3()
{
IplImage* pImage = NULL, *pImage5 = NULL, *pImage9 = NULL, *pImage5_second = NULL;
char* ptr = NULL;
int i = 0;
char* szWindowName[] = {"original", "5x5", "9x9", "5x5 second"};
pImage = cvCreateImage(cvSize(100, 100), 8, 1);
pImage5 = cvCreateImage(cvSize(100, 100), 8, 1);
pImage5_second = cvCreateImage(cvSize(100, 100), 8, 1);
pImage9 = cvCreateImage(cvSize(100, 100), 8, 1);
cvSetZero(pImage);
cvSetZero(pImage5);
cvSetZero(pImage9);
ptr = pImage->imageData + pImage->widthStep * 49 + 50 * pImage->depth * pImage->nChannels/8;
*ptr = 255;
for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i ++)
{
cvNamedWindow(szWindowName[i]);
}
cvSmooth(pImage, pImage5, CV_GAUSSIAN, 5,5);
cvSmooth(pImage5, pImage5_second, CV_GAUSSIAN, 5,5);
cvSmooth(pImage, pImage9, CV_GAUSSIAN, 9,9);
//cvSmooth(pImage, pImage5);
//cvSmooth(pImage, pImage9);
//cvSmooth(pImage5, pImage5_second);
cvMoveWindow(szWindowName[1], 100, 200);
cvShowImage(szWindowName[0], pImage);
cvShowImage(szWindowName[1], pImage5);
cvShowImage(szWindowName[2], pImage9);
cvShowImage(szWindowName[3], pImage5_second);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pImage5);
cvReleaseImage(&pImage9);
cvReleaseImage(&pImage5_second);
cvDestroyAllWindows();
}
void exercise4()
{
IplImage* pImage[8]= {NULL};
int i = 0;
double fSize[3] = {1,4,6};
char* szWindowName[] = {"original", "9-9-x", "0-0-x", "0-0-1-9", "0-0-9-1", "0-0-==>1-9==>9-1", "0-0-9-9","9-9-0-0"};
for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i ++)
{
if (i == 0)
{
pImage[0] = cvLoadImage("mytest.jpg");
}
else
{
pImage[i] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, pImage[0]->nChannels);
}
cvNamedWindow(szWindowName[i]);
}
for (i = 0; i < 3; i ++)
{
cvSmooth(pImage[0], pImage[1], CV_GAUSSIAN, 9,9,fSize[i]);
cvSmooth(pImage[0], pImage[2], CV_GAUSSIAN, 0,0,fSize[i]);
cvShowImage(szWindowName[0], pImage[0]);
cvShowImage(szWindowName[1], pImage[1]);
cvShowImage(szWindowName[2], pImage[2]);
cvWaitKey(0);
}
// c.
cvSmooth(pImage[0], pImage[3], CV_GAUSSIAN, 0,0,1,9);
cvShowImage(szWindowName[3], pImage[3]);
// d.
cvSmooth(pImage[0], pImage[4], CV_GAUSSIAN, 0,0,9,1);
cvShowImage(szWindowName[4], pImage[4]);
// e
cvSmooth(pImage[3], pImage[5], CV_GAUSSIAN, 0,0,9,1);
cvShowImage(szWindowName[5], pImage[5]);
// f.
cvSmooth(pImage[3], pImage[6], CV_GAUSSIAN, 0,0,9,9);
cvShowImage(szWindowName[6], pImage[6]);
cvSmooth(pImage[3], pImage[7], CV_GAUSSIAN, 9,9,0,0);
cvShowImage(szWindowName[7], pImage[7]);
cvShowImage(szWindowName[0], pImage[0]);
//////////////////////////////////////////////////
cvWaitKey(0);
for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i ++)
{
cvReleaseImage(&pImage[i]);
}
cvDestroyAllWindows();
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
exercise4();
return 0;
}
// these are chapter 5 exercises:
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void exercise5()
{
CvCapture* pCapture = NULL;
char* szWinName = "image-diff";
IplImage* pSrc = NULL, * pImage1 = NULL, * pImage2 = NULL, *pDst = NULL;
cvNamedWindow(szWinName);
pCapture = cvCreateCameraCapture(0);
if (pCapture)
{
pSrc = cvQueryFrame(pCapture);
pImage1 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, pSrc->nChannels);
cvCopy(pSrc, pImage1);
cvShowImage(szWinName, pImage1);
cvWaitKey(0);
pSrc = cvQueryFrame(pCapture);
pImage2 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, pSrc->nChannels);
cvCopy(pSrc, pImage2);
cvShowImage(szWinName, pImage2);
cvWaitKey(0);
pDst = cvCreateImage(cvSize(pImage1->width, pImage1->height), pImage1->depth, pImage1->nChannels);
////////////////////////////////////////////////////////////////////////////////////////////////
cvSub(pImage1, pImage2, pDst);
cvShowImage(szWinName, pDst);
cvWaitKey(0);
cvAnd(pImage1, pImage2, pDst);
cvShowImage(szWinName, pDst);
cvWaitKey(0);
cvReleaseImage(&pDst);
cvReleaseImage(&pImage1);
cvReleaseImage(&pImage2);
cvReleaseCapture(&pCapture);
}
cvDestroyWindow(szWinName);
}
void exercise6()
{
CvCapture* pCapture = NULL;
char* szWinName = "image-diff";
IplImage* pImage = NULL, * pImage1 = NULL, * pImage2 = NULL, *pSrc = NULL, *pErodeImage = NULL, *pDilateImage = NULL;
cvNamedWindow(szWinName);
pCapture = cvCreateCameraCapture(0);
if (pCapture)
{
pSrc = cvQueryFrame(pCapture);
pImage1 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, pSrc->nChannels);
cvCopy(pSrc, pImage1);
cvShowImage(szWinName, pImage1);
cvWaitKey(0);
pSrc = cvQueryFrame(pCapture);
pImage2 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, pSrc->nChannels);
cvCopy(pSrc, pImage2);
cvShowImage(szWinName, pImage2);
cvWaitKey(0);
pImage = cvCreateImage(cvSize(pImage1->width, pImage1->height), pImage1->depth, pImage1->nChannels);
////////////////////////////////////////////////////////////////////////////////////////////////
cvSub(pImage1, pImage2, pImage);
cvShowImage(szWinName, pImage);
cvWaitKey(0);
pErodeImage = cvCreateImage(cvSize(pImage1->width, pImage1->height), pImage1->depth, pImage1->nChannels);
////////////////////////////////////////////////////////////////////////////////////////////////
pDilateImage = cvCreateImage(cvSize(pImage1->width, pImage1->height), pImage1->depth, pImage1->nChannels);
////////////////////////////////////////////////////////////////////////////////////////////////
cvErode(pImage, pErodeImage);
cvShowImage(szWinName, pErodeImage);
cvWaitKey(0);
cvDilate(pImage, pDilateImage);
cvShowImage(szWinName, pDilateImage);
cvWaitKey(0);
cvReleaseImage(&pErodeImage);
cvReleaseImage(&pDilateImage);
cvReleaseImage(&pImage);
cvReleaseImage(&pImage1);
cvReleaseImage(&pImage2);
cvReleaseCapture(&pCapture);
}
cvDestroyWindow(szWinName);
}
void exercise7()
{
CvCapture* pCapture = NULL;
char* szWinName = "image-diff";
IplImage* pThreshold = NULL, * pImage1 = NULL, * pImage2 = NULL, *pSrc = NULL, *pDiffImage = NULL, *pMorphImg = NULL;
cvNamedWindow(szWinName);
pCapture = cvCreateCameraCapture(0);
if (pCapture)
{
pSrc = cvQueryFrame(pCapture);
pImage1 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, 1);
cvConvertImage(pSrc, pImage1);
cvShowImage(szWinName, pImage1);
cvWaitKey(0);
pSrc = cvQueryFrame(pCapture);
pImage2 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, 1);
cvConvertImage(pSrc, pImage2);
cvShowImage(szWinName, pImage2);
cvWaitKey(0);
pDiffImage = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, 1);
pThreshold = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, 1);
////////////////////////////////////////////////////////////////////////////////////////////////
pMorphImg = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, 1);
cvAbsDiff(pImage1, pImage2, pDiffImage);
cvShowImage(szWinName, pDiffImage);
cvWaitKey(0);
cvThreshold(pDiffImage, pThreshold, 70, 255, CV_THRESH_BINARY);
cvShowImage(szWinName, pThreshold);
cvWaitKey(0);
cvMorphologyEx(pDiffImage, pMorphImg, NULL, NULL, CV_MOP_CLOSE, 1);
cvShowImage(szWinName, pMorphImg);
cvWaitKey(0);
cvReleaseImage(&pDiffImage);
cvReleaseImage(&pThreshold);
cvReleaseImage(&pMorphImg);
cvReleaseImage(&pImage1);
cvReleaseImage(&pImage2);
cvReleaseCapture(&pCapture);
}
cvDestroyWindow(szWinName);
}
void exercise8()
{
IplImage* pImage[9]= {NULL};
int i = 0;
char* pDst, * pSrc = NULL;
int r;
IplImage* pImg = NULL;
char* szWin = "original ""erode ""dilate " "opening ""closing " "gradient ""tophat ""blackhat " "dummy ";
char* szWindowName[] = {"original", "erode", "dilate", "opening", "closing", "gradient", "tophat","blackhat", "dummy"};
int nNumber = sizeof(szWindowName)/sizeof(char*);
for (i = 0; i < nNumber; i ++)
{
if (i == 0)
{
pImage[0] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
}
else
{
pImage[i] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, 1);
}
cvNamedWindow(szWindowName[i]);
}
cvNamedWindow(szWin);
pImg = cvCreateImage(cvSize(pImage[0]->width*nNumber, pImage[0]->height), pImage[0]->depth, 1);
//pImage[nNumber] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, 1);
cvShowImage(szWindowName[0], pImage[0]);
cvErode(pImage[0], pImage[1]);
cvShowImage(szWindowName[1], pImage[1]);
cvDilate(pImage[0], pImage[2]);
cvShowImage(szWindowName[2], pImage[2]);
cvMorphologyEx(pImage[0], pImage[3], NULL, NULL, CV_MOP_OPEN);
cvShowImage(szWindowName[3], pImage[3]);
cvMorphologyEx(pImage[0], pImage[4], NULL, NULL,CV_MOP_CLOSE);
cvShowImage(szWindowName[4], pImage[4]);
cvMorphologyEx(pImage[0], pImage[5], pImage[8], NULL, CV_MOP_GRADIENT);
cvShowImage(szWindowName[5], pImage[5]);
cvMorphologyEx(pImage[0], pImage[6], NULL, NULL, CV_MOP_TOPHAT);
cvShowImage(szWindowName[6], pImage[6]);
cvMorphologyEx(pImage[0], pImage[7], NULL, NULL, CV_MOP_BLACKHAT);
cvShowImage(szWindowName[7], pImage[7]);
cvShowImage(szWindowName[8], pImage[8]);
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
pDst = pImg->imageData + i*pImage[i]->width;
pSrc = pImage[i]->imageData;
for (r = 0; r < pImage[i]->height; r ++)
{
memcpy(pDst, pSrc, pImage[i]->width);
pDst += pImg->widthStep;
pSrc += pImage[i]->widthStep;
}
cvReleaseImage(&pImage[i]);
}
cvShowImage(szWin, pImg);
cvWaitKey(0);
cvReleaseImage(&pImg);
cvDestroyAllWindows();
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
exercise8();
return 0;
}
// this is a good one which can help you to analysis the image.
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void doExercise9(double fLow, double fHigh)
{
IplImage* pImage[6]= {NULL};
int i = 0;
char* pDst, * pSrc = NULL;
int r;
IplImage* pImg = NULL;
char* szWin = "original " "binary " "binary-inv " "trunc " "tozero-inv " "tozero";
char* szWindowName[] = {"original", "binary", "binary-inv", "trunc", "tozero-inv", "tozero"};
int nNumber = sizeof(szWindowName)/sizeof(char*);
for (i = 0; i < nNumber; i ++)
{
if (i == 0)
{
pImage[0] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
}
else
{
pImage[i] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, 1);
}
cvNamedWindow(szWindowName[i]);
}
cvNamedWindow(szWin);
pImg = cvCreateImage(cvSize(pImage[0]->width*nNumber, pImage[0]->height), pImage[0]->depth, 1);
//pImage[nNumber] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, 1);
cvShowImage(szWindowName[0], pImage[0]);
cvThreshold(pImage[0], pImage[1], fLow, fHigh, CV_THRESH_BINARY);
cvShowImage(szWindowName[1], pImage[1]);
cvThreshold(pImage[0], pImage[2], fLow, fHigh, CV_THRESH_BINARY_INV);
cvShowImage(szWindowName[2], pImage[2]);
cvThreshold(pImage[0], pImage[3], fLow, fHigh, CV_THRESH_TRUNC);
cvShowImage(szWindowName[3], pImage[3]);
cvThreshold(pImage[0], pImage[4], fLow, fHigh, CV_THRESH_TOZERO_INV);
cvShowImage(szWindowName[4], pImage[4]);
cvThreshold(pImage[0], pImage[5], fLow, fHigh, CV_THRESH_TOZERO);
cvShowImage(szWindowName[5], pImage[5]);
//cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
pDst = pImg->imageData + i*pImage[i]->width;
pSrc = pImage[i]->imageData;
for (r = 0; r < pImage[i]->height; r ++)
{
memcpy(pDst, pSrc, pImage[i]->width);
pDst += pImg->widthStep;
pSrc += pImage[i]->widthStep;
}
cvReleaseImage(&pImage[i]);
}
cvShowImage(szWin, pImg);
cvWaitKey(0);
cvReleaseImage(&pImg);
cvDestroyAllWindows();
}
void exercise9()
{
for (int i = 0; i < 10; i ++)
{
doExercise9(i*15, i*20);
}
}
void getImage(IplImage** ppImage)
{
CvCapture* pCapture = NULL;
char* szWinName = "image-diff";
IplImage*pImage1 = NULL, *pImage2 = NULL, *pImage3 = NULL, *pSrc = NULL;
int nChannel = 1;
cvNamedWindow(szWinName);
pCapture = cvCreateCameraCapture(0);
if (pCapture)
{
pSrc = cvQueryFrame(pCapture);
nChannel = 1;
pImage1 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, nChannel);
cvConvertImage(pSrc, pImage1);
cvShowImage(szWinName, pImage1);
cvWaitKey(0);
pSrc = cvQueryFrame(pCapture);
pImage2 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, nChannel);
cvConvertImage(pSrc, pImage2);
cvShowImage(szWinName, pImage2);
cvWaitKey(0);
pImage3 = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, nChannel);
*ppImage = cvCreateImage(cvSize(pSrc->width, pSrc->height), pSrc->depth, nChannel);
cvAbsDiff(pImage1, pImage2, pImage3);
cvThreshold(pImage3, pImage1, 20, 255, CV_THRESH_BINARY);
cvMorphologyEx(pImage1, pImage3, NULL, NULL, CV_MOP_OPEN);
//cvShowImage(szWinName, pImage3);
//cvWaitKey(0);
cvCopy(pImage3, *ppImage);
cvShowImage(szWinName, *ppImage);
cvWaitKey(0);
cvReleaseImage(&pImage1);
cvReleaseImage(&pImage2);
cvReleaseImage(&pImage3);
cvReleaseCapture(&pCapture);
}
cvDestroyWindow(szWinName);
}
bool findImage(IplImage*pImage, char nVal, CvPoint* pPos)
{
char* ptr = NULL;
if (pImage->nChannels == 1)
{
ptr = pImage->imageData;
if (ptr != NULL)
{
for (int r = 0; r < pImage->height; r ++)
{
for (int c = 0; c < pImage->width; c ++)
{
if (ptr[c] == nVal)
{
pPos->x = c;
pPos->y = r;
return true;
}
if (ptr[c]>150)
{
int x = 0;
}
}
ptr += pImage->widthStep;
}
}
}
return false;
}
void exercise10()
{
IplImage* pImg = NULL;
//IplImage* pMaskImg = NULL;
CvPoint oldPoint = {0, 0}, curPoint = {0, 0};
int nArea = 0, newArea = 0;
CvConnectedComp comp;
char* szMyWin = "my win";
getImage(&pImg);
if (pImg != NULL)
{
//pMaskImg = cvCreateImage(cvSize(pImg->width + 2, pImg->height + 2), pImg->depth, 1);
//cvSetZero(pMaskImg);
do
{
if (findImage(pImg, 255, &curPoint))
{
cvFloodFill(pImg, curPoint, cvScalarAll(100), cvScalarAll(0), cvScalarAll(0), &comp, 8|CV_FLOODFILL_FIXED_RANGE);
if (comp.area < nArea)
{
cvFloodFill(pImg, curPoint, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), &comp, 8|CV_FLOODFILL_FIXED_RANGE);
}
else
{
newArea = comp.area;
if (nArea > 0)
{
cvFloodFill(pImg, oldPoint, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), &comp, 8|CV_FLOODFILL_FIXED_RANGE);
}
memcpy(&oldPoint, &curPoint, sizeof(CvPoint));
nArea = newArea;
}
}
else
{
if (nArea > 0)
{
cvFloodFill(pImg, oldPoint, cvScalarAll(255), cvScalarAll(0), cvScalarAll(0), &comp, 8|CV_FLOODFILL_FIXED_RANGE);
}
break;
}
}
while (true);
cvNamedWindow(szMyWin);
cvShowImage(szMyWin, pImg);
cvWaitKey(0);
cvReleaseImage(&pImg);
cvDestroyWindow(szMyWin);
}
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
exercise10();
return 0;
}
// so, this some exercises about cvPyrSegmentation/cvfloodfill etc.
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void exercise11()
{
char* szWinName[] = {"original", "top-hat", "target", "result", "mask"};
IplImage* pImage[5]={NULL};
int i = 0;
for (i = 0; i < sizeof(szWinName)/sizeof(char*); i ++)
{
cvNamedWindow(szWinName[i]);
}
if ((pImage[0] = cvLoadImage("myMask.jpg", CV_LOAD_IMAGE_GRAYSCALE))!= NULL)
{
if ((pImage[1] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, 1))!= NULL)
{
cvMorphologyEx(pImage[0], pImage[1], NULL, NULL, CV_MOP_OPEN);
cvThreshold(pImage[1], pImage[1], 70, 255, CV_THRESH_BINARY);
if ((pImage[2] = cvLoadImage("mytarget.jpg"))!= NULL)
{
//cvResize(pImage[1], pImage[2]);
pImage[4] = cvCreateImage(cvSize(pImage[2]->width, pImage[2]->height), pImage[2]->depth, 1);
cvResize(pImage[1], pImage[4]);
//cvCopy(pImage[1], pImage[4]);
//if (pImage[2]->width >= pImage[3]->width && pImage[2]->height >= pImage[3]->height)
{
//cvSetImageROI(pImage[1], cvRect(0, 0, pImage[2]->width, pImage[2]->height));
pImage[3] = cvCreateImage(cvSize(pImage[2]->width, pImage[2]->height), pImage[2]->depth, pImage[2]->nChannels);
cvSetZero(pImage[3]);
cvCopy(pImage[2], pImage[3], pImage[4]);
for (i = 0; i < sizeof(szWinName)/sizeof(char*); i ++)
{
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
for (i = 0; i < sizeof(szWinName)/sizeof(char*); i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
}
}
}
}
void exercise12()
{
char* szWinName[] = {"original", "nearest neighbour", "bilinear", "pixel area resampling", "bicubic interpolation", "nearest neighbour, bilinear, pixel area resampling, bicubic interpolation"};
IplImage* pImage[6]={NULL};
int newWidth, newHeight, i, nNumber = sizeof(szWinName)/sizeof(char*), nPixelSize = 0;
char* pDst = NULL, *pSrc = NULL;
srand(time(0));
for ( i = 0; i < nNumber; i ++)
{
if (i == 0)
{
if ((pImage[0] = cvLoadImage("mytest.jpg"))!= NULL)
{
newWidth = rand()% 500 + 100;
newHeight = rand()% 500 + 100;
}
else
{
return;
}
}
else
{
if (i == nNumber - 1)
{
pImage[i] = cvCreateImage(cvSize(newWidth*(nNumber - 2), newHeight), pImage[0]->depth, pImage[0]->nChannels);
}
else
{
pImage[i] = cvCreateImage(cvSize(newWidth, newHeight), pImage[0]->depth, pImage[0]->nChannels);
}
}
cvNamedWindow(szWinName[i]);
}
cvResize(pImage[0], pImage[1], CV_INTER_NN);
cvResize(pImage[0], pImage[2], CV_INTER_LINEAR);
cvResize(pImage[0], pImage[3], CV_INTER_AREA);
cvResize(pImage[0], pImage[4], CV_INTER_CUBIC);
nPixelSize = pImage[0]->depth*pImage[0]->nChannels * newWidth / 8;
for (i = 0; i < nNumber; i ++)
{
if (i != 0 && i != nNumber - 1)
{
pSrc = pImage[i]->imageData;
pDst = pImage[nNumber - 1]->imageData + (i-1)*nPixelSize;
for (int r = 0; r < newHeight; r ++)
{
memcpy(pDst, pSrc, nPixelSize);
pDst += pImage[nNumber-1]->widthStep;
pSrc += pImage[i]->widthStep;
}
}
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
void exercise13()
{
char* szWinName[] = {"low variance", "bilateral filter"};
IplImage* pImage = NULL, *pFilterImg = NULL;
int nWidth = 400, nHeight = 400;
char* ptr = NULL;
int nOffset = 40, i;
CvPoint ptStart, ptEnd;
pImage = cvCreateImage(cvSize(nWidth + 1, nHeight + 1), 8, 1);
ptr = pImage->imageData;
for (int r = 0; r < nHeight; r ++)
{
for (int c =0; c < nWidth; c ++)
{
ptr[c] = rand()%4 + 100;
}
ptr += pImage->widthStep;
}
for (i = 0; i <= 10; i ++)
{
ptStart = cvPoint(i*40, 0);
ptEnd = cvPoint(nWidth - i*40, nHeight);
cvLine(pImage, ptStart, ptEnd, cvScalarAll(255));
ptStart = cvPoint(0, i*40);
ptEnd = cvPoint(nWidth, nHeight - i*40);
cvLine(pImage, ptStart, ptEnd, cvScalarAll(255));
}
cvNamedWindow(szWinName[0]);
cvShowImage(szWinName[0], pImage);
pFilterImg = cvCreateImage(cvSize(pImage->width, pImage->height), 8, 1);
cvSmooth(pImage, pFilterImg, CV_BILATERAL, 5,5, 58, 20);
cvNamedWindow(szWinName[1]);
cvShowImage(szWinName[1], pFilterImg);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pFilterImg);
cvDestroyAllWindows();
}
struct MyStruct
{
CvSeq* pSeq;
IplImage* pImage;
IplImage* pRectImage;
};
bool findImage(IplImage* pImage, CvRect rect, CvScalar color, CvPoint* pPoint)
{
unsigned char* ptrRow = NULL, * ptr = NULL;
int nPixelStep = 0;
nPixelStep = pImage->depth * pImage->nChannels / 8;
ptrRow = (unsigned char*)(pImage->imageData + rect.x * nPixelStep + rect.y * pImage->widthStep);
for (int r = 0; r < rect.height; r ++)
{
ptr = ptrRow;
for (int c = 0; c < rect.width; c ++)
{
bool bFind = true;
for (int i = 0; i < nPixelStep; i ++)
{
if (ptr[i] != color.val[i])
{
bFind = false;
break;
}
}
if (bFind)
{
pPoint->x = rect.x + c;
pPoint->y = rect.y + r;
return true;
}
ptr += nPixelStep;
}
ptrRow += pImage->widthStep;
}
return false;
}
char* szMaskWin = "mask window";
void myMouseCallback(int event, int x, int y, int flags, void* pUser)
{
MyStruct* pStruct = (MyStruct*)pUser;
CvSeq* pSeq = NULL;
IplImage* pImage = NULL;
CvConnectedComp* pComp = NULL;
CvRect* pRect = NULL;
char buffer[128];
CvFont myFont = cvFont(1,1);
unsigned char* ptr = NULL;
int nPixelSize, i;
CvScalar color = {0};
CvPoint point;
if (pStruct == NULL)
{
return;
}
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
pSeq = pStruct->pSeq;
pImage = pStruct->pImage;
nPixelSize = pImage->depth * pImage->nChannels / 8;
ptr = (unsigned char*)(pImage->imageData + pImage->widthStep * y + pImage->depth * pImage->nChannels / 8 * x);
for (i = 0; i < pImage->nChannels; i ++)
{
color.val[i] = ptr[i];
}
for (i = 0; i < pSeq->total; i ++)
{
pComp = (CvConnectedComp*)cvGetSeqElem(pSeq, i);
pRect = &pComp->rect;
//if (x > pRect->x && x < pRect->x + pRect->width && y > pRect->y && y < pRect->y + pRect->height)
if (memcmp(&color, &pComp->value, sizeof(CvScalar))==0)
{
/*
cvRectangle(pImage, cvPoint(pRect->x, pRect->y), cvPoint(pRect->x+pRect->width-1, pRect->y+pRect->height-1), pComp->value);
sprintf(buffer, "[area=%.00f][x=%d,y=%d,w=%d,h=%d]", pComp->area, pRect->x, pRect->y, pRect->width, pRect->height);
//cvPutText(pImage, buffer, cvPoint(pRect->x , pRect->y + pRect->height/2), &myFont, cvScalarAll(0));
cvPutText(pImage, buffer, cvPoint(pRect->x, pRect->y+20), &myFont, cvScalarAll(0));
if (pStruct->pRectImage)
{
cvReleaseImage(&pStruct->pRectImage);
}
pStruct->pRectImage = cvCreateImage(cvSize(pRect->width + 1, pRect->height + 1), pImage->depth, 1);
*/
cvSetZero(pStruct->pRectImage);
if (findImage(pImage, *pRect, (CvScalar)pComp->value, &point))
{
//cvSetImageROI(pImage, *pRect);
cvFloodFill(pImage, point, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), NULL, 8|CV_FLOODFILL_FIXED_RANGE|(100<<8), pStruct->pRectImage);
//cvResetImageROI(pImage);
cvShowImage(szMaskWin, pStruct->pRectImage);
}
return;
}
}
// here is problem
sprintf(buffer, "no finding");
cvPutText(pImage, buffer, cvPoint(10,10), &myFont, cvScalarAll(0));
break;
}
}
void exercise14()
{
MyStruct myStruct = {0};
char* szWinName[] = {"original", "input", "segment"};
CvMemStorage* pMem = NULL;
CvSeq* pSeq = NULL;
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
int nSize = 512, i;
pImage[0] = cvLoadImage("mytest.jpg");
pImage[1] = cvCreateImage(cvSize(nSize, nSize), pImage[0]->depth, pImage[0]->nChannels);
pImage[2] = cvCreateImage(cvSize(nSize, nSize), pImage[0]->depth, pImage[0]->nChannels);
cvResize(pImage[0], pImage[1]);
pMem = cvCreateMemStorage();
cvPyrSegmentation(pImage[1], pImage[2], pMem, &pSeq, 5, 100, 30);
for (i = 0; i < sizeof(szWinName)/sizeof(char*); i ++)
{
cvNamedWindow(szWinName[i]);
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
myStruct.pImage = pImage[2];
myStruct.pSeq = pSeq;
cvSetMouseCallback(szWinName[2], myMouseCallback, &myStruct);
myStruct.pRectImage = cvCreateImage(cvSize(myStruct.pImage->width + 2, myStruct.pImage->height + 2), myStruct.pImage->depth, 1);
cvNamedWindow(szMaskWin);
do
{
if (cvWaitKey(500) == 27)
{
break;
}
cvShowImage(szWinName[2], pImage[2]);
}
while (true);
for (i = 0; i < sizeof(szWinName)/sizeof(char*); i ++)
{
cvDestroyWindow(szWinName[i]);
cvReleaseImage(&pImage[i]);
}
cvReleaseMemStorage(&pMem);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
exercise14();
return 0;
}
// I don't understand why Laplace transform looks like this. Probably I made some mistake as I cannot even show the result directly.
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void exercise17()
{
char data1[3][4] =
{
{1,-2,1, 0},
{2,-4,2,0},
{1,-2,1,0},
};
char data2[3][3] =
{
{1,-2,1},
{2,-4,2},
{1,-2,1},
};
CvMat* pMat = NULL;
unsigned char* ptr = NULL;
CvMat myMat;
IplImage* pInput = NULL, * pOutput = NULL, *pImage = NULL;
cvInitMatHeader(&myMat, 3, 3, CV_8SC1, data2, 3);
pMat = cvCreateMat(3, 3, CV_8SC1);
ptr = pMat->data.ptr;
for (int r = 0; r < 3; r ++)
{
for (int c = 0; c < 3; c ++)
{
ptr[c] = data2[r][c];
}
ptr += pMat->step;
}
pInput = cvLoadImage("mytest.jpg");
pOutput = cvCreateImage(cvSize(pInput->width, pInput->height), pInput->depth, pInput->nChannels);
pImage = cvCreateImage(cvSize(pInput->width, pInput->height), pInput->depth, pInput->nChannels);
cvFilter2D(pInput, pOutput, &myMat);
cvFilter2D(pInput, pImage, pMat);
cvNamedWindow("input");
cvNamedWindow("output");
cvNamedWindow("convolve");
cvShowImage("input", pInput);
cvShowImage("output", pOutput);
cvShowImage("convolve", pImage);
cvWaitKey(0);
cvReleaseImage(&pInput);
cvReleaseImage(&pOutput);
cvReleaseImage(&pImage);
cvDestroyAllWindows();
}
void exercise19();
void exercise18()
{
char* pSrc = NULL, *pDst = NULL;
char* szWinName[] = {"input", "output10","output20", "output01","output11", "output21", "output02","output12", "output22", "paranoma"};
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
int i = 0, x, y, index = 0, nNumber = sizeof(szWinName)/sizeof(char*);
for (i = 0; i < nNumber; i ++)
{
if (i ==0 )
{
pImage[i] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
}
else
{
if (i == nNumber-1)
{
pImage[i] = cvCreateImage(cvSize(pImage[0]->width*(nNumber-1), pImage[0]->height), pImage[0]->depth, pImage[0]->nChannels);
}
else
{
pImage[i] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, pImage[0]->nChannels);
}
}
cvNamedWindow(szWinName[i]);
}
for (y = 0; y < 3; y ++)
{
for (x = 0; x < 3; x ++)
{
index = y*3+x;
if (x!=0 || y != 0)
{
cvSobel(pImage[0], pImage[index], x, y);
}
cvShowImage(szWinName[index], pImage[index]);
}
}
for (i = 0; i < nNumber - 1; i ++)
{
pSrc = pImage[i]->imageData;
pDst = pImage[nNumber - 1]->imageData + pImage[i]->width*i;
for (y = 0; y < pImage[i]->height; y ++)
{
memcpy(pDst, pSrc, pImage[i]->width);
pSrc += pImage[i]->widthStep;
pDst += pImage[nNumber - 1]->widthStep;
}
}
cvShowImage(szWinName[nNumber - 1], pImage[nNumber - 1]);
//exercise19();
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
void exercise19()
{
char*szWinName[] = {"original", "laplace"};
IplImage* pImage[3] = { NULL };
char* pDst = NULL, *pDstRow, *pSrcRow;
char* pSrc = NULL;
short* pSrcShort = NULL;
int i;
int nSrcPix, nDstPix;
pImage[0] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (pImage[0]->depth == 8)
{
pImage[1] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), IPL_DEPTH_16S, pImage[0]->nChannels);
pImage[2] = cvCreateImage(cvSize(pImage[0]->width, pImage[0]->height), pImage[0]->depth, pImage[0]->nChannels);
cvLaplace(pImage[0], pImage[1]);
//cvConvertImage(pImage[1], pImage[2]);
pSrcRow = pImage[1]->imageData;
pDstRow = pImage[2]->imageData;
nSrcPix = (pImage[1]->depth^IPL_DEPTH_SIGN)*pImage[1]->nChannels/8;
nDstPix = pImage[2]->depth*pImage[2]->nChannels/8;
for (int r = 0; r < pImage[1]->height; r ++)
{
pDst = pDstRow;
pSrc = pSrcRow;
for (int c = 0; c < pImage[1]->width; c ++)
{
pSrcShort = (short*)pSrc;
for (int n = 0; n < pImage[0]->nChannels; n ++)
{
pDst[n] = pSrcShort[n];
}
pDst += nDstPix;
pSrc += nSrcPix;
}
pDstRow += pImage[2]->widthStep;
pSrcRow += pImage[1]->widthStep;
}
for (i = 0; i < 2; i ++)
{
cvNamedWindow(szWinName[i]);
}
cvShowImage(szWinName[0], pImage[0]);
cvShowImage(szWinName[1], pImage[2]);
cvWaitKey(0);
for (i = 0; i < 2; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
exercise19();
return 0;
}
//the exercise of chapter 6
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#pragma comment(lib, "cvd.lib")
#pragma comment(lib, "cxcored.lib")
#pragma comment(lib, "highguid.lib")
void exercise20(bool bFirst)
{
char* szWinName[] = {"original", "3x3", "5x5", "7x7"};//, "9x9", "11x11", "13x13"};
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
int nWidth = 600, nHeight = 600;
int n = 0, i = 0, nNumber = sizeof(szWinName)/sizeof(char*);
for (n = 0; n < nNumber; n ++)
{
pImage[n] = cvCreateImage(cvSize(nWidth, nHeight), 8, 3);
if (n== 0)
{
cvZero(pImage[n]);
for (i = 0; i <= 12; i ++)
{
cvCircle(pImage[n], cvPoint(nWidth/2, nHeight/2), i*25, cvScalar(i%3*i*50, (i+1)%3*i*50, (i+2)%3*50));
cvLine(pImage[n], cvPoint(i*50, 0), cvPoint(nWidth-i*50, nHeight-1), cvScalarAll(255));
cvLine(pImage[n], cvPoint(0, i*50), cvPoint(nWidth-1, nHeight-i*50), cvScalarAll(255));
}
}
else
{
if (true)
{
//cvSobel(pImage[0], pImage[n], 1, 0, (n-1)*2+3);
//cvLaplace(pImage[0], pImage[n], (n-1)*2 + 3);
}
else
{
//cvSobel(pImage[0], pImage[n], 0, 1, (n-1)*2+3);
}
}
cvNamedWindow(szWinName[n]);
cvShowImage(szWinName[n], pImage[n]);
}
cvWaitKey(0);
for (n = 0; n < nNumber; n ++)
{
cvReleaseImage(&pImage[n]);
cvDestroyWindow(szWinName[n]);
}
}
void exercise21()
{
char* szWinName[] = {"original", "circles"};
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
CvMemStorage* pMem = NULL;
int n = 0, i = 0, nNumber = sizeof(szWinName)/sizeof(char*);
CvSeq* pSeq = NULL;
float* pNumber = NULL;
pMem = cvCreateMemStorage();
for (i = 0; i < nNumber; i ++)
{
switch (i)
{
case 0:
pImage[i] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
break;
case 1:
pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]->depth, pImage[0]->nChannels);
cvZero(pImage[i]);
cvSmooth(pImage[0], pImage[i], CV_GAUSSIAN, 5, 5);
pSeq = cvHoughCircles(pImage[i], pMem, CV_HOUGH_GRADIENT, 2, pImage[i]->width/10);
for (n = 0; n < pSeq->total; n ++)
{
pNumber = (float*)cvGetSeqElem(pSeq, n);
CvPoint pt = cvPoint(cvRound(pNumber[0]), cvRound(pNumber[1]));
cvCircle(pImage[i], pt, cvRound(pNumber[2]), CV_RGB(255, 255, 255));
}
break;
}
cvNamedWindow(szWinName[i]);
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
cvReleaseMemStorage(&pMem);
}
void exercise22()
{
#define PI 3.1415926
char* szWinName[] = {"original", "lines", "canny"};
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
CvMemStorage* pMem = NULL;
int n = 0, i = 0, nNumber = sizeof(szWinName)/sizeof(char*);
CvSeq* pSeq = NULL;
CvPoint* ptr = NULL;
pMem = cvCreateMemStorage();
for (i = 0; i < nNumber; i ++)
{
switch (i)
{
case 0:
pImage[i] = cvLoadImage("mytest.jpg", CV_LOAD_IMAGE_GRAYSCALE);
break;
case 1:
pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]->depth, pImage[0]->nChannels);
cvZero(pImage[i]);
cvSmooth(pImage[0], pImage[i], CV_GAUSSIAN, 5, 5);
//1, CV_PI/180, 50, 50, 10 );
pSeq = cvHoughLines2(pImage[i], pMem, CV_HOUGH_PROBABILISTIC, 100, PI/18.0, 250, 350, 0);
for (n = 0; n < pSeq->total; n ++)
{
ptr = (CvPoint*)cvGetSeqElem(pSeq, n);
cvLine(pImage[i], ptr[0], ptr[1], cvScalarAll(0));
}
break;
case 2:
pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]->depth, pImage[0]->nChannels);
cvZero(pImage[i]);
cvCanny(pImage[0], pImage[i], 10, 50, 5);
}
cvNamedWindow(szWinName[i]);
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
void exercise23()
{
float alpha = 0.866f, beta = 0.5f, x = 200.0f, y = 200.0f;
float pData[6] = {0.866025f, 0.5, 0.0f, -0.5f, 0.866025f, 0.0f};
char* szWinName[] = {"original", "warpAffine"};
IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
int n = 0, i = 0, nNumber = sizeof(szWinName)/sizeof(char*);
CvMat myMat = {0};
CvMat* pMat = cvCreateMat(2, 3, CV_32FC1);
//pData[2] = (1-alpha)*x - beta*y;
//pData[5] = beta*x + (1-alpha)*y;
cvInitMatHeader(&myMat, 2, 3, CV_32FC1, pData, sizeof(float)*3);
for (i = 0; i < nNumber; i ++)
{
switch (i)
{
case 0:
pImage[i] = cvLoadImage("mytest.jpg");
break;
case 1:
pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]->depth, pImage[0]->nChannels);
cv2DRotationMatrix(cvPoint2D32f(x, y), 60, 1.0, pMat);
//cvWarpAffine(pImage[0], pImage[1], pMat);
cvWarpAffine(pImage[0], pImage[1], &myMat);
break;
}
cvNamedWindow(szWinName[i]);
cvShowImage(szWinName[i], pImage[i]);
}
cvWaitKey(0);
for (i = 0; i < nNumber; i ++)
{
cvReleaseImage(&pImage[i]);
cvDestroyWindow(szWinName[i]);
}
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
exercise23();
return 0;
}