#include#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv; using namespace std; static void on_imgThresholded(int, void*); int iLowH = 0; int iHighH = 75; int iLowS = 0; int iHighS = 255; int iLowV = 0; int iHighV = 255; Mat imgThresholded; Mat imgOriginal; Mat imgHSV; vector hsvSplit; void on_imgThresholded(int, void*) { inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image //开操作 (去除一些噪点) Mat element = getStructuringElement(MORPH_RECT, Size(5, 5)); morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element); //闭操作 (连接一些连通域) morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element); imshow("Thresholded Image", imgThresholded); //show the thresholded image imshow("Original", imgOriginal); //show the original image } int main() { imgOriginal = imread("pyrMeanShiftFiltering_RGB.jpg"); namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control" cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做 split(imgHSV, hsvSplit); equalizeHist(hsvSplit[2], hsvSplit[2]); merge(hsvSplit, imgHSV); //imshow("HSV",imgHSV); createTrackbar("LowH", "Control", &iLowH, 179, on_imgThresholded); //Hue (0 - 179) createTrackbar("HighH", "Control", &iHighH, 179, on_imgThresholded); createTrackbar("LowS", "Control", &iLowS, 255, on_imgThresholded); //Saturation (0 - 255) createTrackbar("HighS", "Control", &iHighS, 255, on_imgThresholded); createTrackbar("LowV", "Control", &iLowV, 255, on_imgThresholded); //Value (0 - 255) createTrackbar("HighV", "Control", &iHighV, 255, on_imgThresholded); on_imgThresholded(0, 0); waitKey(0); return 0; }