Draw a white rectangle using the region growth method

Firstly, the region growth method in digital image processing is briefly introduced

Three problems need to be considered in the application of region growth method

1. What are the seed pixels or which one

The general principles for selecting and determining a set of seed pixels are:
(1) Pixels close to the cluster center can be used as seed pixels

(2) The brightest pixel in infrared image target detection can be used as seed pixel

(3) Determine the seed pixel according to the location requirements

(4) Determine the seed pixel based on some experience

2. Determine the similarity criterion that can merge adjacent pixels in the growth process

The criteria for determining the similarity criteria mainly include:

(1) Consider the needs of the problem you want to deal with. For example, if you need to find points with similar gray level, you should consider whether the average value of the points to be detected and all pixels in the merged area meet some similarity criteria. Generally speaking, that is, whether the gray level value meets the conditions you want to merge in.

(2) Size requirements, shape requirements, image color, etc

3. Determine the conditions or rules for terminating the growth process

(1) The general stop growth criterion is that the growth process proceeds until the pixels that do not meet the growth criterion

Of course, you can also add the requirements required by the problem you are dealing with, such as shape requirements, size requirements, etc

In this paper, a white rectangular frame is drawn by region growth method, which is equivalent to setting a shape restriction condition for an image, and assigning the gray value to the pixels of the image.

Explain with the three questions just introduced:
Seed pixel: a point in the image manually selected by yourself

Similarity criterion: meet the image shape

Conditions for termination of growth: collect the white rectangular box

The original intention of this example is to give readers a preliminary understanding of regional growth

The following is the program to draw a white rectangular box. There is still a large optimization space for the class method of region growth defined in it, so let the reader optimize it.

import numpy as np
import cv2


class RegionGrow:
    """The region grows into a rectangular box"""
    def __init__(self, x, y, width, length, img_height, img_width):
        """The point of region growth and the height and width setting of the image"""
        self.x = x
        self.y = y
        #   Custom lineweight
        self.width = width
        #   Custom line length
        self.length = length
        #    Generate a black image
        self.img = np.zeros([img_height, img_width])
        self.img_height = img_height
        self.img_width = img_width

    def grow_length(self):
        print(self.img.shape)
        self.label = 255
        length_source = 1#  Initial value of rectangle length
        for length in range(1, self.length):
            """Draw the left length and the top length of the rectangle"""
            self.line_new_x = self.x + length_source
            self.line_new_y = self.y + length_source
            if self.line_new_x < self.img_height - 20 and self.line_new_y < self.img_width - 20:
                #   Region growth in the x direction
                self.img[self.line_new_x, self.y] = self.label
                #   Region growth in the y direction
                self.img[self.x, self.line_new_y] = self.label
                length_source += 1
        print(f"{self.line_new_x}-{self.y},{self.x}-{self.line_new_y} ")
        length_source_1 = 1
        for length_1 in range(1, self.length):
            """Draw the length of the lower side and the length of the right side"""
            self.line_new_y1 = self.y + length_source_1
            self.line_new_x1 = self.x + length_source_1
            if self.line_new_x1 < self.img_height - 20 and self.line_new_y1 < self.img_width - 20:
                #   After the region grows to a new starting point, continue according to the new starting point, which is the lower side length of the rectangle
                self.img[self.line_new_x, self.line_new_y1] = self.label
                #   After the region grows to a new starting point, continue according to the new starting point, which is the length on the right side of the rectangle
                self.img[self.line_new_x1, self.line_new_y] = self.label
                length_source_1 += 1
        print(f"{self.line_new_x}--{self.line_new_y1}, {self.line_new_x1}--{self.line_new_y}")

    def grow_width(self):
        """Changes the width of the painted rectangular line from 1 pixel to self.width Pixel size"""
        i = 1
        for length in range(self.x, self.line_new_x + 1):
            width = self.width
            i = 1
            while width > 0:
                """Draw the left length and the top length of the rectangle"""
                """Widens the left length of the rectangle"""
                self.img[length, self.y + i] = self.label
                """Widens the top length of the rectangle"""
                self.img[self.x + i, length] = self.label
                """Widens the right length of the rectangle"""
                self.img[length, self.line_new_y - i] = self.label
                """Widens the bottom length of the rectangle"""
                self.img[self.line_new_x - i, length] = self.label
                i += 1
                width -= 1


img_region_grow = RegionGrow(20, 20, 50, 760, 800, 800)
img_region_grow.grow_length()
img_region_grow.grow_width()
cv2.imshow('img_region_grow', img_region_grow.img)
cv2.imwrite('C:\\Users\\yu\\Desktop\\picture_csdn\\img_region_a_line.png', img_region_grow.img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The effect diagram is as follows:

A new black image in the program

 

 

Image after region growth

 

Keywords: Python OpenCV image processing

Added by paul.mac on Mon, 24 Jan 2022 16:18:09 +0200