Opencv——Mat像素算術操做

基於像素點的算術操做ios

加add、減subtract、乘multiply、除divideide

saturate_cast<T>(value)spa

#include <opencv2/opencv.hpp>ip

#include <iostream>input

using namespace cv;it

using namespace std;io

int main(int artc, char** argv) {opencv

         Mat src1 = imread("D:/test1.jpg");ast

         Mat src2 = imread("D:/test2.jpg");test

         if (src1.empty() || src2.empty()) {

                  printf("could not load image...\n");

                  return -1;

         }

         namedWindow("input", CV_WINDOW_AUTOSIZE);

         imshow("input1", src1);

         imshow("input2", src2);

         int height = src1.rows;

         int width = src1.cols;

         int b1 = 0, g1 = 0, r1 = 0;

         int b2 = 0, g2 = 0, r2 = 0;

         int b = 0, g = 0, r = 0;

         Mat result = Mat::zeros(src1.size(), src1.type());

         for (int row = 0; row < height; row++) {

                  for (int col = 0; col < width; col++) {

                                   b1 = src1.at<Vec3b>(row, col)[0];//訪問像素值

                                   g1 = src1.at<Vec3b>(row, col)[1];

                                   r1 = src1.at<Vec3b>(row, col)[2];

 

                                   b2 = src2.at<Vec3b>(row, col)[0];

                                   g2 = src2.at<Vec3b>(row, col)[1];

                                   r2 = src2.at<Vec3b>(row, col)[2];

                                   result.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b1 + b2);//像素值類型轉換

                                   result.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g1 + g2);

                                   result.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r1 + r2);

                  }

         }

         imshow("output", result);

 

         Mat add_result = Mat::zeros(src1.size(), src1.type());

         add(src1, src2, add_result);//圖像加法操做

         imshow("add_result", add_result);

 

         Mat sub_result = Mat::zeros(src1.size(), src1.type());

         subtract(src1, src2, sub_result);//圖像減法操做

         imshow("sub_result", sub_result);

 

         Mat mul_result = Mat::zeros(src1.size(), src1.type());

         multiply(src1, src2, mul_result);//圖像乘法操做

         imshow("mul_result", mul_result);

 

         Mat div_result = Mat::zeros(src1.size(), src1.type());

         divide(src1, src2, div_result);//圖像除法操做

         imshow("div_result", div_result);

        

         waitKey(0);

         return 0;

}

相關文章
相關標籤/搜索