机器视觉课程的第一个assignment——OpenCV

这个assignment很简单。。。而且我也是第一次写OpenCV,我使用的是windows的microsoft visual studio 2008 pro and 2.3.1 OpenCV,把代码贴出来和大家共享~~喵呜~~

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>

#define CV_IMAGE_ELEM( image, elemtype, row, col ) \
(((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])

using namespace std;

IplImage *srcImage = 0;
IplImage *tempImage = 0;
IplImage *roiImage=0;
char chek_cut_state=0;
CvRect ROI_rect;
CvPoint origin;

void on_mouse( int event, int x, int y, int flags, void* param ) ;

//----------main function-----------------

void main( )
{
// read a color image and display it in a window
srcImage = cvLoadImage("foo.jpg",-1);
cvNamedWindow( "Original Image", 1 );
cvShowImage( "Original Image",srcImage);

// capture mouse motion when a rectangle is dragged in the image
cvSetMouseCallback( "Original Image", on_mouse, 0 );

// user press 'q' key to quit
char c;
while(1)
{
c = cvWaitKey(0);
if(c =='q') break;
}

cvReleaseImage(&roiImage);
cvReleaseImage(&srcImage);
cvDestroyWindow("Original Image");
cvDestroyWindow("Final Image");
}


//call back function which is called every time a mouse event occurs in the specified window

void on_mouse( int event, int x, int y, int flags, void* param )

{
int thickness=1; //the thickness of the line when a rectangle is drawn in the image

CvScalar color = CV_RGB(255,0,0); //the color of the line

//obtain the coordinates of one corner of the rectangle when the left button of the mouse is down
if( (event==CV_EVENT_LBUTTONDOWN) )
{
origin = cvPoint(x,y);
ROI_rect = cvRect(x,y,0,0);
chek_cut_state=1;
}

//dynamically draw the rectangle as mouse moves
if( chek_cut_state && event==CV_EVENT_MOUSEMOVE)
{
tempImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 1);
tempImage = cvCloneImage(srcImage);

cvRectangle( tempImage, origin, cvPoint(x,y), color,thickness, CV_AA, 0);
cvShowImage( "Original Image", tempImage );
cvReleaseImage(&tempImage);
}

//obtain the coordinates of the other corner of the rectangle when the left button if the mouse is up
if( chek_cut_state && event==CV_EVENT_LBUTTONUP )
{
chek_cut_state=0;

//calculate the coordinates of the left corner,wdith and height of the rectangle
ROI_rect.x = MIN(x,origin.x);
ROI_rect.y = MIN(y,origin.y);
ROI_rect.width = abs(x - origin.x);
ROI_rect.height = abs(y - origin.y);

//make a copy of selected rectangular region and write it to a PNG file
cvSetImageROI(srcImage, ROI_rect); //set ROI
roiImage = cvCreateImage(cvSize(ROI_rect.width,ROI_rect.height),8,3);
cvCopy(srcImage,roiImage);
cvResetImageROI(srcImage);
cvSaveImage("foo.rect.png",roiImage);

uchar b, g, r; // 3 channels
int row=0, col=0; //loop variables

//then exchange its red and green channels and write that to a JPEG file
for(row = 0; row < roiImage->height; row++)
{
for (col = 0; col < roiImage->width; col++)
{
g = CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 1);
r = CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 2);
CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 1) = r;
CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 2) = g;

}
}
cvSaveImage("foo.rect.rgswap.jpg",roiImage);

//then subtract the blue channel values from 255 and write that to a PNG file
for(row = 0; row < roiImage->height; row++)
{
for (col = 0; col < roiImage->width; col++)
{
b = CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 0);
b = 255-b;
CV_IMAGE_ELEM(roiImage, uchar, row, col * roiImage->nChannels + 0) = b;
}
}
cvSaveImage("foo.rect.rgswap.invb.png",roiImage);

//paste that final result into the original image to create a new image in PNG format
cvSetImageROI(srcImage, ROI_rect);
cvCopy(roiImage,srcImage);
cvResetImageROI(srcImage);
cvSaveImage("foo.modified.jpg",srcImage);
cvNamedWindow("Final Image",1);
cvShowImage("Final Image",srcImage);
}

}

也顺便请教下大牛们。。。我在网上看了一些OpenCV的程序还有tutorial里面的一些程序。。。发现头文件的包含非常不同。。。当我用网上很多方法,比如#include<highgui.h>的时候就会显示找不到或者什么的。。。我感觉我把include里面的头文件都加载了。。。哪位大牛能帮我解答一下疑惑~~~而且我还有个问题。。。用Mat和IplImage存储图像有什么区别捏~

原文地址:https://www.cnblogs.com/meinvlv/p/2658344.html