opencv笔记

来源:互联网 发布:windows系统log 编辑:程序博客网 时间:2024/05/19 00:54



显示图像


#include"opencv2/highgui/highgui.hpp"  
  
int main()  
{  
    IplImage* img = cvLoadImage("123.jpg");  
    cvNamedWindow("Demo1",CV_WINDOW_AUTOSIZE);  
    cvShowImage("Demo1", img);  
      
    cvWaitKey(0);  
    cvReleaseImage(&img);  
    cvDestroyWindow("Demo1");

    return 0;

}

cvNamedWindow()第一个参数指定了该窗口的标题,同时也作为该窗口的索引。第二个参数可选0或CV_WINDOW_AUTOSIZE,为0时图像根据窗口大小进行伸缩,为CV_WINDOW_AUTOSIZE时,窗口根据图片大小伸缩。


#include"opencv2/highgui/highgui.hpp"  
using namespace cv;  
  
int main()  
{  
    Mat image = imread("123.jpg");  
    imshow("Demo2", image);  
    cvWaitKey(0);  
}  


imread和cvloadimage有什么异同
OpenCV提供C接口和C++接口,功能是一样的。选用何种接口,完全取决于个人爱好。
如果你的程序是C风格的,最好选用C接口,如果是C++风格的,最好选用C++接口。
C接口的很多函数需要配对使用,比如:创建矩阵后需要释放矩阵,相对来说,代码量要大一些,一个不小心就会出现内存泄漏。


在图像上画矩形框

cvRectangle与cv::rectangle的用法cvRentangle和cv::rectangle函数原型对比:C:       void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )C++: void rectangle(Mat& img, Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0)C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )参数介绍:img图像.pt1矩形的一个顶点。pt2矩形对角线上的另一个顶点color线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。thickness组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。line_type线条的类型。见cvLine的描述shift坐标点的小数点位数。代码:#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;}

如果需要在Mat类型的图上绘制矩形,选择cv::trctangle()在IplImage*类型的图上绘制矩形,选择cvRectangle()

opencv document

OpenCV Tutorials

opencv中文网站

OpenCV基础数据结构

学习OPENCV(二):操作像素

访问IplImage数据时的注意事项

图像处理和图像识别中常用的OpenCV函数(整理)

OpenCV学习笔记(9)利用MFC的Picture控件显示图像+播放视频和捕获摄像头画面

OpenCV中使用神经网络 CvANN_MLP

OpenCV - Mat、CvMat、IplImage类型浅析

Cximage<---->Mat 、Cximage<---->lplImage 以及 lplImage<----->Mat的转换、像素位深度

OpenCV:初试牛刀-带滚动条的视频播放-2

截取图像

#include "opencv2/imgproc/imgproc.hpp"  #include "opencv2/highgui/highgui.hpp"  using namespace cv;// Define our callback which we will install for  // mouse events.  //  void my_mouse_callback(int event, int x, int y, int flags, void* param);CvRect box;bool drawing_box = false;bool isRectDrawn = false;// A litte subroutine to draw a box onto an image_copy  void draw_box(IplImage* img, CvRect rect) {cvRectangle(img,cvPoint(box.x, box.y),cvPoint(box.x + box.width, box.y + box.height),cvScalar(0xff, 0x00, 0x00), /* blue */2);}void draw_box_green(IplImage* img, CvRect rect) {cvRectangle(img,cvPoint(box.x, box.y),cvPoint(box.x + box.width, box.y + box.height),cvScalar(0x00, 0xff, 0x00), /* green */2);}// This is our mouse callback. If the user  // presses the left button, we start a box.  // when the user releases that button, then we  // add the box to the current image_copy. When the  // mouse is dragged (with the button down) we  // resize the box.  //  void my_mouse_callback(int event, int x, int y, int flags, void* param){IplImage* image_copy = (IplImage*)param;switch (event) {case CV_EVENT_MOUSEMOVE:{   if (drawing_box) {   box.width = x - box.x;   box.height = y - box.y;   }}break;case CV_EVENT_LBUTTONDOWN: {   drawing_box = true;   box = cvRect(x, y, 0, 0);}break;case CV_EVENT_LBUTTONUP: { drawing_box = false; isRectDrawn = true; if (box.width < 0) { box.x += box.width; box.width *= -1; } if (box.height < 0) { box.y += box.height; box.height *= -1; } draw_box(image_copy, box);}break;}}int main(int argc, char* argv[]){box = cvRect(-1, -1, 0, 0);IplImage* image_input = cvLoadImage(argv[1]);IplImage* image = cvCloneImage(image_input);IplImage* image_copy = cvCloneImage(image);IplImage* temp = cvCloneImage(image_copy);cvNamedWindow(argv[1], 0);// Here is the crucial moment that we actually install  // the callback. Note that we set the value ‘param’ to  // be the image_copy we are working with so that the callback  // will have the image_copy to edit.  //  cvSetMouseCallback(argv[1],my_mouse_callback,(void*)image_copy);// The main program loop. Here we copy the working image_copy  // to the ‘temp’ image_copy, and if the user is drawing, then  // put the currently contemplated box onto that temp image_copy.  // display the temp image_copy, and wait 15ms for a keystroke,  // then repeat…  //  while (1) {//cvCopyImage( image_copy, temp );  cvCopy(image_copy, temp);if (drawing_box) draw_box(temp, box);cvShowImage(argv[1], temp);//if( cvWaitKey( 15 )==27 ) break;  int key = cvWaitKey(15);if (key == 27) break;if (isRectDrawn){if (key == 's' || key == 'S'){// draw green box  draw_box_green(image_copy, box);cvCopy(image_copy, image);// save roi image  static int index = 0;char save_image_name[128];string sa = string(argv[1]);string fn=string(sa.begin()+sa.length()-10,sa.end()-4);sprintf(save_image_name, (fn+string("rect_%d.jpg")).c_str(), index++);cvSetImageROI(image_input, box);cvSaveImage(save_image_name, image_input);cvResetImageROI(image_input);isRectDrawn = false;}if (key == 'q' || key == 'Q'){cvCopy(image, image_copy);isRectDrawn = false;}}}// Be tidy  //  cvReleaseImage(&image_copy);cvReleaseImage(&temp);cvDestroyWindow("Box Example");}


opencv中各种矩阵乘的区别

修改opencv源码的方法









0 0