1. 程式人生 > >學習c++版opencv3.4之4

學習c++版opencv3.4之4

1.新建空圖

2.完全複製影象clone和copyTo

3.獲取影象畫素值

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(){
    Mat src;
    src = imread("/Users/ming/Documents/test.jpg");
    if (! src.data){
        printf("cannot load image...");
    }
    namedWindow("src img", CV_WINDOW_AUTOSIZE);
    imshow("src img", src);
    
    /* 新建空圖 */
    Mat dst;
    dst = Mat::zeros(src.size(), src.type()); //定義一張空圖(純黑色)
    dst = Scalar(127, 0, 255); //修改每個通道值,bgr分別是127,0,255
    namedWindow("new img", CV_WINDOW_AUTOSIZE);
    imshow("new img", dst);
    /* 克隆clone影象,是完全複製,賦值操作只能複製頭部 */
    Mat dst2 = src.clone();
    namedWindow("clone img", CV_WINDOW_AUTOSIZE);
    imshow("clone img", dst2);
    /* copyTo複製影象,與clone相同,是完全複製,賦值操作只能複製頭部 */
    Mat dst3;
    src.copyTo(dst3);
    namedWindow("copyTo img", CV_WINDOW_AUTOSIZE);
    imshow("copyTo img", dst3);
    /*獲取畫素值*/
    cvtColor(src, src, CV_BGR2GRAY);
    namedWindow("gray img", CV_WINDOW_AUTOSIZE);
    imshow("gray img", src);
    const uchar* first_row = src.ptr<uchar>(0); //獲取畫素值
    printf("first pixel row value: %d\n", *first_row);
    /*新建空圖2*/
    Mat M(100,100, CV_8UC3, Scalar(0,0,255));
//    cout << "M=" << M << endl;
    cout << "size of M =" << M.size << endl;
    namedWindow("red img", CV_WINDOW_AUTOSIZE);
    imshow("red img", M);
    waitKey(0);
}