现在的位置: 首页 > 综合 > 正文

opencv从图像中截取部分作为子图像

2018年05月18日 ⁄ 综合 ⁄ 共 1229字 ⁄ 字号 评论关闭

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

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
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

抱歉!评论已关闭.