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