1.读取并显示图片
#include "opencv2\opencv.hpp" using namespace cv; Mat img = imread("../../kk030873.jpg"); if (img.empty()) { fprintf(stderr, "Can not load image\n"); } imshow("image", img); waitKey(30);C语言有时候导入"opencv2\opencv.hpp"有问题,可以用下面的方法,只导入部分.h文件即可
#include "opencv2/core/core_c.h" #include "opencv2/highgui/highgui_c.h" #include "opencv2/imgproc/imgproc_c.h" CvMat* pstFgImg = cvCreateMatHeader(_pstIm->iHeight, _pstIm->iWidth, CV_8UC1); pstFgImg->data.ptr = pstHandle->pstImage->pucD1; cvShowImage("Fg Img", pstFgImg); cvReleaseMat(&pstFgImg);2.读取视频
#include "opencv2\opencv.hpp" using namespace cv; capture.open("../001.avi"); if (!capture.isOpened()) { printf("open error \n"); } while (true) { capture >> frame; if (frame.empty()) { printf("frame is empty \n"); break; } img = cv::Mat(frame.rows, frame.cols, CV_8UC3); img.data = frame.data; cv::imshow("det", img); cv::waitKey(1); } capture.release();3.改变图片大小
#include "opencv2\opencv.hpp" using namespace cv; Mat img; Mat frame_res; img =imread("../../kk030873.jpg"); resize(img, frame_res, Size(),0.3,0.3); imshow("读取视频", frame_res); waitKey(30);4.在图像上画框
#include "opencv2/core/core_c.h" #include "opencv2/highgui/highgui_c.h" #include "opencv2/imgproc/imgproc_c.h" CvMat* pstFgImg = cvCreateMatHeader(_pstI->iHeight, _pstI->iWidth, CV_8UC1); pstFgImg->data.ptr = pstHandle->pstImage->pucD1; CvPoint stStart = { pstTgtInfo->stRect.left, pstTgtInfo->stRect.top }; CvPoint stEnd = { pstTgtInfo->stRect.right, pstTgtInfo->stRect.bottom }; cvRectangle(pstFgImg, stStart, stEnd, CV_RGB(255,255,255), 1, 8, 0); cvShowImage("Fg Img", pstFgImg); cvReleaseMat(&pstFgImg);5.在图片上标记框之cvRectangle与cv::rectangle的区别
#include <iostream> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; int main() { char *imageSrc = "I:\\OpenCV Learning\\picture\\sumpalace.jpg"; Mat matImage = imread(imageSrc,-1); IplImage *iplImage = cvLoadImage(imageSrc,-1); if(matImage.data==0||iplImage->imageData ==0) { cout<<"图片加载失败"<<endl; return -1; } cv::rectangle(matImage,cvPoint(20,200),cvPoint(200,300),Scalar(255,0,0),1,1,0); //Rect(int a,int b,int c,int d)a,b为矩形的左上角坐标,c,d为矩形的长和宽 cv::rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0); cvRectangle(iplImage,cvPoint(20,200),cvPoint(200,300),Scalar(0,255,255),1,1,0); imshow("matImage",matImage); cvShowImage("IplImage",iplImage); waitKey(); return 0; }