OpenCV | 基本操作API

来源:互联网 发布:手机淘宝没有收藏按钮 编辑:程序博客网 时间:2024/04/20 12:36
#MAT cons
cv::Mat img;
cv::Mat img1(1000, 1000, CV_8UC3, cv::Scalar(0, 100, 255));//cv::Scalar(里面是通道数的赋值)
因为是unsigned char Channel 3,所以Scalar是三个数值
cv::Mat img2 = img;//浅拷贝
cv::Mat img2(img);//浅拷贝
cv::Mat img3 = img.clone()//深拷贝
img.copyTo(img3)//深拷贝

img.rows
img.cols
cv::Mat img=cv::imread("i.jpg");if(!img.data) return -1;
cv::namedWindow("Image")
cv::imshow("namedWindow",img)
img.type() == CV_8UC1 "灰色" CV_8UC3 "彩色"
//RGB彩色转GRAY灰度图
cv::cvtColor(image, img_gray, CV_BGR2GRAY);
//GRAY灰度图才可以转MASK,把threshold_value(80)到256的转成黑色0,其他为白色255
cv::threshold(img_gray, img_mask, threshold_value, max_BINARY_value=256, THRESH_BINARY);
img.copyTo(out,img_mask);//img原图,在img_mask的白色点,用原图点覆盖,img_mask黑色点,还是黑色点

img.at<unsigned char>(row,col) = 255 对灰色某个点赋值白色
img.at<cv::Vec3b>(row,col)[0] = 255 对彩色某个点赋值白色,彩色是3个[0][1][2]
img.at<cv::Vec3b>(row,col)[1] = 255
img.at<cv::Vec3b>(row,col)[2] = 255
更简单一些的方法:OpenCV定义了一个Mat的模板子类为Mat_,它重载了operator()让我们可以更方便的取图像上的点。
Mat_<uchar> im=image;
im(i,j)=im(i,j)/div*div+div/2;

###########

#IplImage

IplImage* image=cvLoadImage("D:\\123.jpg",-1);//-1 表示按照图像本身的类型来读取,1 表示强制彩色化,0 表示强制灰值化.
cvSaveImage("filename",IplImage);
cvShowImage("图像显示",image);
cvReleaseImage(&image);


#highgui windows窗口
namedWindow(string,flag)
destroyWindow(const String& winname)
destroyAllWindows()
startWindowThread()
waitKey(int delay = 0)//delay in milliseconds, 0 = forever
imshow(const String& winname, Mat)
resizeWindow(const String& winname, int width, int height)
moveWindow(const String& winname, int x, int y)
updateWindow(const String& winname)
setWindowProperty(const String& winname, int prop_id, double prop_value)
enum WindowPropertyFlags {//for prop_id
        WND_PROP_FULLSCREEN = 0, //!< fullscreen property    (can be WINDOW_NORMAL or WINDOW_FULLSCREEN).
        WND_PROP_AUTOSIZE = 1, //!< autosize property      (can be WINDOW_NORMAL or WINDOW_AUTOSIZE).
        WND_PROP_ASPECT_RATIO = 2, //!< window's aspect ration (can be set to WINDOW_FREERATIO or WINDOW_KEEPRATIO).
        WND_PROP_OPENGL = 3  //!< opengl support.
};
enum WindowFlags {//for prop_value
    WINDOW_NORMAL = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
    WINDOW_AUTOSIZE = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.
    WINDOW_OPENGL = 0x00001000, //!< window with opengl support.

    WINDOW_FULLSCREEN = 1,          //!< change the window to fullscreen.
    WINDOW_FREERATIO = 0x00000100, //!< the image expends as much as it can (no ratio constraint).
    WINDOW_KEEPRATIO = 0x00000000  //!< the ratio of the image is respected.

};

setWindowTitle(const String& winname, const String& title)
double getWindowProperty(const String& winname, int prop_id)

##########
#trackbar
##########
createTrackbar
getTrackbarPos(const String& trackbarname, const String& winname)
setTrackbarPos(const String& trackbarname, const String& winname, int pos)
setTrackbarMax(const String& trackbarname, const String& winname, int maxval)
setTrackbarMin(const String& trackbarname, const String& winname, int minval)


#########
#iterator
#########
cv::MatIterator_<cv::Vec3b> it   = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator it = image.end<cv::Vec3b>();
//从第二行开始
it=image.begin<cv::Vec3b>()+image.cols

(*it)[0] = ????
(*it)[1] = ????
(*it)[2] = ????

cv::Mat_<cv::Vec3b> cimage(image);
cv::MatIterator_<cv::Vec3b> it = cimage.begin()/cimage.end()


########################
##count time/ per second
const int64 start = cv::getTickCount();
foo();
double duration = (cv::getTickCount()-start)/cv::getTickFrequency();

#############
#图像简单运算
#############
1. 图像叠加
cv::add(img,img1,output);
cv::addWeighted(img,0.9,img2,0.2,0.1,output)//output=0.9*img+0.2*img2+scalar(0.1)

cv::subtract
cv::bitwise_and
cv::bitwise_not
cv::bitwise_or/bitwise_xor
2. 图像截取一块
Mat image;
cv::Rect rect(cv::Point(x1, y1), cv::Point(x2, y2));
Mat res = image(rect);
或者:
Mat imageROI = image(Rect(x1,y1,length,height))

3. 拉伸或缩小全图
res.create(image.rows*2, image.cols * 2, CV_8UC3);
cv::resize(image, res, res.size());
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )

4. 多边形
 CvMat *Mat = cvCreateMat(nMatWidth,nMatHeight,CV_8UC3);
 CvPoint Points[5] = { {12,45}, {78,150}, {240,240}, {132,132}, {200,230} };
 cvFillConvexPoly(Mat,Points,5,CV_RGB(255,255,255));
 cvShowImage("Show_Result",Mat);
 cvReleaseMat(&Mat);

0 0
原创粉丝点击