Draw a white rectangle using the region growing method

First, a brief introduction to the region growing method in digital image processing

When applying the region growing method, three issues need to be considered

1. Which or which seed pixels are

The general principles for selecting and determining a group of representative 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 a seed pixel

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

(4) Determine the seed pixel based on some experience

2. Determine the similarity criterion that can incorporate adjacent pixels during the growth process

The criteria for determining similarity criteria mainly include:

(1) Consider the needs of the problem you want to deal with, for example, if you need to find a point with similar grayscale, then you should consider whether the average value of the point to be detected and all the pixels in the merged area satisfies a certain similarity criterion, In layman's terms, it means that the size of the gray value does not meet the conditions you want to enter.

(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 there are no pixels that meet the growth criterion

Of course, it can also be determined by adding the requirements required by the problem you are dealing with, such as shape requirements, size requirements, etc.

This paper uses the region growing method to draw a white rectangular frame, which is equivalent to setting a shape constraint condition for an image, and assigning gray value to the pixels.

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

Similarity criterion: just satisfy the shape of the image

Conditions for terminating growth: just complete the white rectangle

The original intention of giving this example is to let readers have a more preliminary understanding of regional growth

The following is a program to realize the drawing of a white rectangular frame. There is still a lot of room for optimization in the class method of the defined region growth, and it is left to the reader to optimize.

import numpy as np
import cv2


class RegionGrow:
    """区域生长成一条矩形框"""
    def __init__(self, x, y, width, length, img_height, img_width):
        """区域生长的点以及图像的高宽设置"""
        self.x = x
        self.y = y
        #   自定义的线宽
        self.width = width
        #   自定义的线长
        self.length = length
        #    生成一个黑色图像
        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#  矩形框长度初值
        for length in range(1, self.length):
            """将矩形的左边长和上边长绘画出来"""
            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:
                #   在x方向上进行区域生长
                self.img[self.line_new_x, self.y] = self.label
                #   在y方向上进行区域生长
                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):
            """将举行的下边长以及右边长进行绘画出来"""
            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:
                #   在区域增长到一个新的起点之后,按照新的起点继续,此为矩形的下边长
                self.img[self.line_new_x, self.line_new_y1] = self.label
                #   在区域增长到一个新的起点之后,按照新的起点继续,此为矩形的右边长
                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):
        """将绘画的矩形线宽度从1像素变为self.width大小的像素"""
        i = 1
        for length in range(self.x, self.line_new_x + 1):
            width = self.width
            i = 1
            while width > 0:
                """将矩形的左边长和上边长绘画出来"""
                """将矩形的左边长加宽"""
                self.img[length, self.y + i] = self.label
                """将矩形的上边长加宽"""
                self.img[self.x + i, length] = self.label
                """将矩形的右边长加宽"""
                self.img[length, self.line_new_y - i] = self.label
                """将矩形的下边长加宽"""
                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 realized effect diagram is as follows:

A new black image created in the program

 

 

Image after region growing

 

Guess you like

Origin blog.csdn.net/kuwola/article/details/122630424