首页 > 代码库 > OpenCV 学习

OpenCV 学习

技术分享
  1 #include <opencv2\opencv.hpp>  2 #include <iostream>  3 #include <opencv2\highgui\highgui.hpp>  4 #include <Windows.h>  5 using namespace std;  6 using namespace cv;  7   8 const int Silder_Max = 64;  9 int Silder; 10 Mat Image; 11 Mat Result; 12 BOOL bLBtnDown = FALSE; 13 BOOL bLBtnUp = FALSE; 14 Point Corner1, Corner2; 15 Rect box; 16 VOID ColorReduce(Mat& InputImage, Mat& OutputImage, int div,int Func); 17 static VOID Mouse_CallBack(int Event, int x, int y, int, VOID*); 18 VOID On_TrackBar(int Pos,VOID*); 19 VOID Sub_1(); 20 int main() 21 { 22     //    Mat picture = imread("1.jpg");//图片必须添加到工程目录下 23     //                                          //也就是和test.cpp文件放在一个文件夹下!!! 24     //    imshow("测试程序", picture); 25     //    waitKey(20150901); 26     //} 27     Image = imread("3.jpg"); 28     namedWindow("原图像"); 29     namedWindow("显示结果"); 30     namedWindow("Cropping app"); 31     Silder = 0; 32     createTrackbar("ColorReduce", "显示结果", &Silder, Silder_Max, On_TrackBar); 33     setMouseCallback("原图像", Mouse_CallBack); 34     imshow("原图像", Image); 35     imshow("显示结果", Image); 36     waitKey(0); 37 } 38  39 #pragma region 鼠标事件回调 40  41 static VOID Mouse_CallBack(int Event, int x, int y, int, VOID*) 42 { 43     if (Event == EVENT_LBUTTONDOWN) 44     { 45         bLBtnDown = TRUE; 46         Corner1.x = x; 47         Corner1.y = y; 48         cout << Corner1 << endl; 49     } 50     if (Event == EVENT_LBUTTONUP) 51     { 52         if (abs(x - Corner1.x) > 20 && abs(y - Corner1.y) > 20) 53         { 54             bLBtnUp = TRUE; 55             Corner2.x = x; 56             Corner2.y = y; 57             cout << Corner2 << endl; 58         } 59         else 60         { 61             bLBtnDown = FALSE; 62         } 63     } 64     if (bLBtnDown == TRUE && bLBtnUp == FALSE) 65     { 66         Point pt; 67         pt.x = x; 68         pt.y = y; 69         Mat Local_Image = Image.clone(); 70         rectangle(Local_Image, Corner1, pt, Scalar(0, 0, 255)); 71  72         imshow("Cropping app", Local_Image); 73     } 74     if (bLBtnDown == TRUE && bLBtnUp == TRUE) 75     { 76         box.width = abs(Corner1.x - Corner2.x); 77         box.height = abs(Corner1.y - Corner2.y); 78         box.x = min(Corner1.x, Corner2.x); 79         box.y = min(Corner1.y, Corner2.y); 80         Mat crop(Image, box); 81         Mat Temp; 82         ColorReduce(crop, Temp, 0, 1); 83         namedWindow("Crop"); 84         imshow("Crop", Temp); 85         bLBtnDown = FALSE; 86         bLBtnUp = FALSE; 87  88     } 89 } 90  91  92 #pragma endregion 93 #pragma region 滑竿空间操作 94 VOID On_TrackBar(int Pos, VOID*) 95 { 96     if (Pos <= 0) 97     { 98         Result = Image; 99     }100     else101     {102         ColorReduce(Image, Result, Pos, 0);103     }104     imshow("显示结果", Result);105 }106 #pragma endregion107 108 #pragma region 色素操作109 110 VOID ColorReduce(Mat& InputImage, Mat& OutputImage, int div, int Func)111 {112 113     if (Func == 0)114     {115         OutputImage = InputImage.clone();116         int Rows = OutputImage.rows;117         int Cols = OutputImage.cols * OutputImage.channels();118         if (OutputImage.isContinuous())119         {120             Cols *= Rows;121             Rows = 1;122         }123         for (int i = 0;i < Rows;i++)124         {125             UCHAR* Data = http://www.mamicode.com/InputImage.ptr(i);126             UCHAR* DataOut = OutputImage.ptr<UCHAR>(i);127             for (int j = 0;j < Cols;j++)128             {129                 Data[j] = DataOut[j] / div*div + div / 2;130             }131         }132     }133     else134     {135         OutputImage = InputImage.clone();136         int Rows = OutputImage.rows;137         int Cols = OutputImage.cols;138         for (int i = 0;i < Rows;i++)139         {140             for (int j = 0;j < Cols;j++)141             {142                 OutputImage.at<Vec3b>(i, j)[0] = 255 - OutputImage.at<Vec3b>(i, j)[0];143                 OutputImage.at<Vec3b>(i, j)[1] = 255 - OutputImage.at<Vec3b>(i, j)[1];144                 OutputImage.at<Vec3b>(i, j)[2] = 255 - OutputImage.at<Vec3b>(i, j)[2];145 146             }147         }148     }149 150 }151 #pragma endregion152 153 VOID Sub_1()154 {155 156 157     Mat Image, Result;158     cout << "Size: " << Image.size().height << "," << Image.size().width << endl;159     Image = imread("1.jpg");160     if (!Image.data)161     {162 163         cout << "Read Data Failed" << endl;164     }165     namedWindow("Ortginal Image");166     imshow("Ortginal Image", Image);167     flip(Image, Result, 1);168     namedWindow("Output Image");169     imshow("Output Image", Result);170     waitKey(0);171 }
View Code

 

OpenCV 学习