?鼠标右键取样,作为灰度分界线进行最简单的二值化处理
效果图:
?
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
bool flag = false;
Mat image;
int val1, val2, val3;
void mouse_call_back(int event, int x, int y, int flags, void *userdata);
int main(int argc, char *argv[]) {
namedWindow("img", 0);
image = imread("/home/rubo/1.jpg");
if (image.empty()) {
cout << "no picture!" << endl;
return 0;
}
cvtColor(image, image, COLOR_RGB2GRAY, 0);
// inRange(image, Scalar(0), Scalar(100), image);
imshow("img", image);
setMouseCallback("img", mouse_call_back, 0);
waitKey(0);
return 0;
}
void mouse_call_back(int event, int x, int y, int flags, void *userdata) {
switch (event) {
case EVENT_RBUTTONDOWN: {
flag = 1;
break;
}
case EVENT_RBUTTONUP: {
if (flag) {
val1 = image.at<Vec3b>(y, x)[0];
// val2 =image.at<Vec3b>(y, x)[1];
// val3 = image.at<Vec3b>(y, x)[2];
cout << val1 << endl;
Mat result;
inRange(image, Scalar(0), Scalar(val1), result);
namedWindow("result", 0);
imshow("result", result);
}
flag = 0;
break;
}
}
}
|