opencv 截取图片一部分_opencv从图像中截取部分作为子图像 | 学步园

简单的小例程,留作用从图像中截取部分作为子图像,并保存起来

#include

#include

#include

#include

using namespace std;

void GetImageRect(IplImage* orgImage, CvRect rectInImage, IplImage* imgRect);

int main(int argc , char** argv )

{

IplImage* src;

src = cvLoadImage(argv[1], 0);

cvNamedWindow("src", 1);

cvShowImage("src", src);

IplImage* dst;

CvRect rect = cvRect(0, 0, 0.5*src->width, 0.5*src->height);

CvSize dst_size;

dst_size.height = rect.height;

dst_size.width = rect.width;

dst = cvCreateImage(dst_size, IPL_DEPTH_8U, src->nChannels);

GetImageRect(src, rect, dst);

cvNamedWindow("dst",2);

cvShowImage("dst",dst);

cvSaveImage("dst.jpg", dst);

cvWaitKey(0);

cvDestroyWindow("src");

cvDestroyWindow("dst");

cvReleaseImage(&src);

cvReleaseImage(&dst);

return 0;

}

void GetImageRect(IplImage* orgImage, CvRect rectInImage, IplImage* imgRect)

{

//从图像orgImage中提取一块rectInImage子图像imgRect

//IplImage *result = imgRect;

CvSize size;

size.width = rectInImage.width;

size.height = rectInImage.height;

//从图像中提取子图像

cvSetImageROI(orgImage, rectInImage);

cvCopy(orgImage, imgRect);

cvResetImageROI(orgImage);

}

下面是一个makefile

CFLAGS = `pkg-config --cflags opencv`

LIBS = `pkg-config --libs opencv`

obj = harris.o

codebook: $(obj)

g++ $(CFLAGS) -g -o harris $(obj) $(LIBS)

harris.o:harris.cpp

g++ $(CFLAGS) -g -c harris.cpp $(LIBS)

.PHONY:clean

clean:

rm *.o codebook

你可能感兴趣的:(opencv,截取图片一部分)