1. /*-------------全局变量----------------*/
    2. int value;
    3. void callBack(int, void*);
    4. Mat img1, img2;
    5. /*--------------主函数-----------------*/
    6. int main()
    7. {
    8. system("color F0");
    9. ReadMat(img1, "D:/OpenCV/watch.jpg");
    10. value = 100;
    11. namedWindow("改变亮度");
    12. createTrackbar("亮度值百分比", "改变亮度", &value, 600, callBack, 0);
    13. waitKey(0);
    14. return 0;
    15. }
    16. /*-------------函数实现----------------*/
    17. static void callBack(int, void*)
    18. {
    19. float a = value / 100.0;
    20. img2 = img1 * a;
    21. imshow("改变亮度", img2);
    22. }
    1. #define WINDOW "原图"
    2. /*-------------全局变量----------------*/
    3. Mat g_srcImage, g_dstImage;
    4. Point previousPoint;
    5. bool P = false;
    6. void On_mouse(int event, int x, int y, int flags, void*);
    7. /*--------------主函数-----------------*/
    8. int main()
    9. {
    10. system("color F0");
    11. Mat img1,sobel,scharr,canny,laplace;
    12. ReadMat(g_srcImage, "D:/OpenCV/ycy2.jpg");
    13. imshow(WINDOW, g_srcImage);
    14. setMouseCallback(WINDOW, On_mouse, 0);
    15. waitKey(0);
    16. return 0;
    17. }
    18. /*-------------函数实现----------------*/
    19. void On_mouse(int event, int x, int y, int flags, void*)
    20. {
    21. switch (event)
    22. {
    23. case EVENT_LBUTTONUP:
    24. {
    25. P = false;
    26. }
    27. break;
    28. case EVENT_LBUTTONDOWN:
    29. {
    30. previousPoint = Point(x, y);
    31. P = true;
    32. }
    33. break;
    34. case EVENT_MOUSEMOVE:
    35. {
    36. if (P)
    37. {
    38. Point pt(x, y);
    39. line(g_srcImage, previousPoint, pt, Scalar(0, 0, 255), 2, 5, 0);
    40. previousPoint = pt;
    41. imshow(WINDOW, g_srcImage);
    42. }
    43. }
    44. break;
    45. }
    46. }
    1. vector<Mat> bgr_planes;
    2. split(src, bgr_planes);
    3. int histsize = 256;
    4. float range[] = { 0,256 };
    5. const float* histRanges = { range };
    6. Mat b_hist, g_hist, r_hist;
    7. calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histsize, &histRanges, true, false);
    8. calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histsize, &histRanges, true, false);
    9. calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histsize, &histRanges, true, false);
    10. int hist_h = 400;//直方图的图像的高
    11. int hist_w = 512;// 直方图的图像的宽
    12. int bin_w = hist_w / histsize;//直方图的等级
    13. Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));//绘制直方图显示的图像
    14. normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());//归一化
    15. normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
    16. normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
    17. for (int i = 1; i < histsize; i++)
    18. {
    19. //绘制蓝色分量直方图
    20. line(histImage, Point((i - 1) * bin_w, hist_h - cvRound(b_hist.at<float>(i - 1))),
    21. Point((i)*bin_w, hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2);
    22. //绘制绿色分量直方图
    23. line(histImage, Point((i - 1) * bin_w, hist_h - cvRound(g_hist.at<float>(i - 1))),
    24. Point((i)*bin_w, hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2);
    25. //绘制红色分量直方图
    26. line(histImage, Point((i - 1) * bin_w, hist_h - cvRound(r_hist.at<float>(i - 1))),
    27. Point((i)*bin_w, hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2);
    28. }
    1. /*--------------main-----------------*/
    2. int main()
    3. {
    4. system("color F0");
    5. Mat src,gray;
    6. ReadMat(src, "D:/OpenCV/ycy.jpg");
    7. Mat canny;
    8. Canny(src, canny, 50, 100);
    9. GaussianBlur(canny, canny, Size(3, 3), 10, 10);
    10. Mat struct0 = getStructuringElement(1, Size(3, 3));
    11. erode(canny, canny, struct0);
    12. vector<vector<Point>> contours;
    13. findContours(canny, contours, RETR_CCOMP, CHAIN_APPROX_NONE);
    14. for (int n = 0; n < contours.size(); n++)
    15. {
    16. double area = contourArea(contours[n]);
    17. if (area >= 1000)
    18. {
    19. Rect rect = boundingRect(contours[n]);
    20. rectangle(src, rect, Scalar(0, 0, 255), 2);
    21. RotatedRect rrect = minAreaRect(contours[n]);
    22. Point2f points[4];
    23. rrect.points(points);
    24. Point2f cpt = rrect.center;
    25. for (int i = 0; i < 4; i++)
    26. {
    27. if (i == 3)
    28. {
    29. line(src, points[i], points[0], Scalar(0, 255, 0), 2);
    30. break;
    31. }
    32. line(src, points[i], points[i + 1], Scalar(0, 255, 0), 2);
    33. }
    34. }
    35. else
    36. {
    37. continue;
    38. }
    39. }
    40. resize(src, src, Size(600, 600));
    41. imshow("1", src);
    42. waitKey(0);
    43. return 0;
    44. }
    1. /*--------------main-----------------*/
    2. int main()
    3. {
    4. system("color F0");
    5. Mat src,gray;
    6. ReadMat(src, "D:/OpenCV/ycy2.jpg");
    7. cvtColor(src, gray, COLOR_BGR2GRAY);
    8. imshow("0", gray);
    9. Mat harris;
    10. int blockSize = 2;
    11. int apertureSize = 3;
    12. cornerHarris(gray, harris, blockSize, apertureSize, 0.04);
    13. Mat harrisn;
    14. normalize(harris, harrisn, 0, 255, NORM_MINMAX);
    15. convertScaleAbs(harrisn, harrisn);
    16. vector<KeyPoint> kpts;
    17. for (int row = 0; row < harrisn.rows; row++)
    18. {
    19. for (int col = 0; col < harrisn.cols; col++)
    20. {
    21. int R = harrisn.at<uchar>(row, col);
    22. if (R > 200)
    23. {
    24. KeyPoint kpt;
    25. kpt.pt.x = row;
    26. kpt.pt.y = col;
    27. kpts.push_back(kpt);
    28. }
    29. }
    30. }
    31. drawKeypoints(src, kpts, src);
    32. imshow("1", src);
    33. waitKey(0);
    34. return 0;
    35. }
    1. int main()
    2. {
    3. system("color F0");
    4. Mat src,gray;
    5. ReadMat(src, "D:/OpenCV/ycy2.jpg");
    6. Mat copy;
    7. ReadMat(copy, "D:/OpenCV/ycy2_in_scene.jpg");
    8. vector<KeyPoint> Keypoints1, Keypoints2;
    9. Mat descriptions1, description2;
    10. orb_features(src, Keypoints1, descriptions1);
    11. orb_features(copy, Keypoints2, description2);
    12. vector<DMatch> matches;
    13. BFMatcher matcher(NORM_HAMMING);
    14. matcher.match(descriptions1, description2, matches);
    15. cout << "numbers:" << matches.size() << endl;
    16. //汉明距离筛选
    17. double min_dist = 10000, max_dist = 0;
    18. for (int i = 0; i < matches.size(); i++)
    19. {
    20. double dist = matches[i].distance;
    21. if (dist < min_dist) min_dist = dist;
    22. if (dist > max_dist) max_dist = dist;
    23. }
    24. vector<DMatch> goodmatches;
    25. for (int i = 0; i < matches.size(); i++)
    26. {
    27. if (matches[i].distance <= std::max(2*min_dist,20.0))
    28. {
    29. goodmatches.push_back(matches[i]);
    30. }
    31. }
    32. Mat outimg, outhamimg;
    33. drawMatches(src, Keypoints1, copy, Keypoints2, matches, outimg);
    34. drawMatches(src, Keypoints1, copy, Keypoints2, goodmatches, outhamimg);
    35. resize(outimg, outimg, Size(), 0.3, 0.3);
    36. resize(outhamimg, outhamimg, Size(), 0.3, 0.3);
    37. imshow("1", outimg);
    38. imshow("2", outhamimg);
    39. waitKey(0);
    40. return 0;
    41. }
    1. int main(int argc, char* argv[])
    2. {
    3. Mat camera;
    4. CascadeClassifier cascade;
    5. string faceCascadeName = "D:/OpenCV/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml";
    6. cascade.load(faceCascadeName);
    7. VideoCapture cap(0);
    8. if (!cap.isOpened())
    9. {
    10. cout << "No video!" << endl;
    11. return -1;
    12. }
    13. while (true)
    14. {
    15. cap >> camera;
    16. FaceDetection(camera, cascade);
    17. imshow("camera", camera);
    18. if (waitKey(10) == 27)
    19. {
    20. break;
    21. }
    22. }
    23. cap.release();
    24. destroyAllWindows();
    25. return 0;
    26. }

    1.mp4 (2.33MB)