YouTip LogoYouTip

Cpp Opencv Advanced Image Processing

Advanced image processing refers to achieving more complex tasks on the basis of basic image processing (such as filtering, edge detection, etc.), such as image segmentation, contour detection, morphological operations, histogram processing, etc. These technologies are widely used in object detection, image analysis, medical image processing and other fields. ### Application Scenarios of Advanced Image Processing **Medical Image Analysis:** * Use image segmentation techniques to extract lesion areas. * Use morphological operations to remove noise. **Object Detection and Tracking:** * Use contour detection and template matching to locate objects. * Use histogram processing to enhance object features. **Image Enhancement:** * Use histogram equalization to improve image contrast. * Use watershed algorithm to separate overlapping objects. * * * ## Image Segmentation Image segmentation is an important step in image processing, which divides an image into multiple regions or objects for further analysis and processing. Common image segmentation methods include threshold-based segmentation, edge-based segmentation, and region-based segmentation. ### Threshold-based Segmentation Threshold-based segmentation is one of the simplest image segmentation methods. It divides the pixel values of an image into different categories by setting one or more threshold values. Common threshold segmentation methods include global threshold and adaptive threshold. #### Global Threshold Segmentation Global threshold segmentation uses a fixed threshold to divide an image into foreground and background. OpenCV provides the `cv::threshold` function to implement this function. cv::Mat src = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE); cv::Mat dst; cv::threshold(src, dst, 128, 255, cv::THRESH_BINARY); In the above code, `128` is the threshold, `255` is the maximum pixel value, and `cv::THRESH_BINARY` indicates the use of the binarization method. #### Adaptive Threshold Segmentation Adaptive threshold segmentation dynamically adjusts the threshold based on local regions of the image. OpenCV provides the `cv::adaptiveThreshold` function to implement this function. cv::Mat src = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE); cv::Mat dst; cv::adaptiveThreshold(src, dst, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 11, 2); In the above code, `255` is the maximum pixel value, `cv::ADAPTIVE_THRESH_MEAN_C` indicates using the local mean as the threshold, `11` is the neighborhood size, and `2` is the constant. ### Edge-based Segmentation Edge-based segmentation segments an image by detecting edges in the image. Common edge detection algorithms include Canny edge detection and Sobel operator. #### Canny Edge Detection Canny edge detection is a multi-stage edge detection algorithm. OpenCV provides the `cv::Canny` function to implement this function. cv::Mat src = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE); cv::Mat edges; cv::Canny(src, edges, 100, 200); In the above code, `100` and `200` are the two thresholds of the Canny algorithm. ### Region-based Segmentation (Watershed Algorithm) Region-based segmentation achieves segmentation by dividing an image into multiple regions. The watershed algorithm is a commonly used region-based segmentation method. #### Watershed Algorithm The watershed algorithm treats the image as a topographic map and segments the image by simulating water flow diffusion. OpenCV provides the `cv::watershed` function to implement this function. cv::Mat src = cv::imread("image.jpg"); cv::Mat markers = cv::Mat::zeros(src.size(), CV_32S); cv::watershed(src, markers); In the above code, `markers` is the marker matrix used to store the segmentation results. * * * ## Contour Detection Contour detection is an important step in image processing, which is used to detect object boundaries in an image. OpenCV provides various functions for contour detection and processing. ### Finding Contours Finding contours is the first step in contour detection. OpenCV provides the `cv::findContours` function to implement this function. cv::Mat src = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE); std::vector<std::vector> contours; std::vector hierarchy; cv::findContours(src, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); In the above code, `contours` is the vector storing contours, and `hierarchy` is the vector storing the contour hierarchy. ### Contour Features Contour features include area, perimeter, bounding box, etc. OpenCV provides various functions to calculate these features. #### Area Area is an important feature of a contour. OpenCV provides the `cv::contourArea` function to calculate the area of a contour. double area = cv::contourArea(contours); #### Perimeter Perimeter is another important feature of a contour. OpenCV provides the `cv::arcLength` function to calculate the perimeter of a contour. double perimeter = cv::arcLength(contours, true); #### Bounding Box The bounding box is the minimum enclosing rectangle of a contour. OpenCV provides the `cv::boundingRect` function to calculate the bounding box. cv::Rect rect = cv::boundingRect(contours); ### Contour Drawing Contour drawing is the process of drawing detected contours onto an image. OpenCV provides the `cv::drawContours` function to implement this function. cv::Mat dst = cv::Mat::zeros(src.size(), CV_8UC3); cv::drawContours(dst, contours, -1, cv::Scalar(0, 255, 0), 2); In the above code, `dst` is the image after drawing contours, `cv::Scalar(0, 255, 0)` is the color of the contours, and `2` is the line width of the contours. * * * ## Template Matching Template matching is a method for finding a specific template in an image. OpenCV provides the `cv::matchTemplate` function to implement this function. ### Single Template Matching Single template matching is finding one template in an image. OpenCV provides the `cv::matchTemplate` function to implement this function. cv::Mat src = cv::imread("image.jpg"); cv::Mat templ = cv::imread("template.jpg"); cv::Mat result; cv::matchTemplate(src, templ, result, cv::TM_CCOEFF_NORMED); In the above code, `result` is the matching result matrix, and `cv::TM_CCOEFF_NORMED` is the matching method. ### Multi-Template Matching Multi-template matching is finding multiple templates in an image. Multi-template matching can be achieved by iterating through the matching result matrix. double minVal, maxVal; cv::Point minLoc, maxLoc; cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc); In the above code, `maxLoc` is the position of the maximum value of the matching result. * * * ## Examples ### Watershed Algorithm ## Examples #include #include using namespace cv; using namespace std; int main(){ // Read image Mat image = imread("objects.jpg"); if(image.empty()){ cout<<"Error: Unable to load image, please check if the path is correct."<< endl; return-1; } // Convert to grayscale image Mat gray; cvtColor(image, gray, COLOR_BGR2GRAY); // Binarization Mat binary; threshold(gray, binary, 0, 255, THRESH_BINARY_INV + THRESH_OTSU); // Remove noise Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); morphologyEx(binary, binary, MORPH_OPEN, kernel, Point(-1, -1), 2); // Calculate distance transform Mat dist; distanceTransform(binary, dist, DIST_L2, 5); // Normalize distance transform image normalize(dist, dist, 0, 1.0, NORM_MINMAX); // Binarize distance transform image threshold(dist, dist, 0.5, 1.0, THRESH_BINARY); // Find contours Mat dist_8u; dist.convertTo(dist_8u, CV_8U); vector<vector> contours; findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // Create marker image Mat markers = Mat::zeros(dist.size(), CV_32S); for(size_t i =0; i < contours.size(); i++){ drawContours(markers, contours, static_cast(i), Scalar(static_cast(i)+1), -1); } // Apply watershed algorithm watershed(image, markers); // Display result Mat
← Cpp Opencv Ml Deep LearningCpp Opencv Basic Modules β†’