An example of region growth for automatic selection of initial seeds (Python + openCV)

Time:2020-10-18

In the algorithm, the initial seed can be automatically selected (different seeds can be obtained through different partition, and the algorithm can be improved according to your own needs). The images are the original image (I drew two strokes to divide into different regions), gray histogram, initial seed image, and region growth result map.

In addition, the threshold selection is very important regardless of initial seed selection or region growth.

import cv2
import numpy as np
import matplotlib.pyplot as plt

#Initial seed selection
def originalSeed(gray, th):
 ret, thresh = cv2.cv2.threshold(gray, th, 255, cv2.THRESH_ Binary diagram, seed region (different division can obtain different seeds)
 kernel = cv2.getStructuringElement(cv2.MORPH_ Ellipse, (3,3)) ා 3 × 3 structural element

 thresh_ copy =  thresh.copy () ා copy thresh_ A to thresh_ copy
 thresh_ B =  np.zeros ( gray.shape , np.uint8 ) #thresh_ B is the same size as a, with a pixel value of 0

 Seeds = [] ා to record seed coordinates

 #Loop until thresh_ All pixel values in copy are 0
 while thresh_copy.any():

  Xa_ copy, Ya_ copy =  np.where (thresh_ copy > 0) #thresh_ A_ Copy coordinates of pixels with a median value of 255
  thresh_ B[Xa_ copy[0], Ya_ Copy [0]] = 255 ා, select the first point, and set thresh_ The corresponding pixel value in B is changed to 255

  #Connected component algorithm, first of all, for thresh_ B inflates, and then performs and operation with thresh (take intersection)
  for i in range(200):
   dilation_B = cv2.dilate(thresh_B, kernel, iterations=1)
   thresh_B = cv2.bitwise_and(thresh, dilation_B)

  #Take thresh_ B value is 255 pixel coordinates, and thresh is set_ The corresponding coordinate pixel value in copy becomes 0
  Xb, Yb = np.where(thresh_B > 0)
  thresh_copy[Xb, Yb] = 0

  #Loop, in thresh_ Stop when there is only one pixel in B
  while str(thresh_B.tolist()).count("255") > 1:
   thresh_ B = cv2.erode(thresh_ B. Kernel, iterations = 1) ා corrosion operation

  X_ seed, Y_ seed =  np.where (thresh_ B > 0) ා seed coordinates of the location
  if X_seed.size > 0 and Y_seed.size > 0:
   seeds.append ((X_ seed[0], Y_ Seed [0])) ා write seed coordinates to seeds
  thresh_ B [XB, Yb] = 0 ා thresh_ B pixel value zero
 return seeds

#Regional growth
def regionGrow(gray, seeds, thresh, p):
 seedMark = np.zeros(gray.shape)
 #Eight neighborhoods
 if p == 8:
  connection = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
 elif p == 4:
  connection = [(-1, 0), (0, 1), (1, 0), (0, -1)]

 #The growth stops when there is no element in seeds
 while len(seeds) != 0:
  #Stack top element out of stack
  pt = seeds.pop(0)
  for i in range(p):
   tmpX = pt[0] + connection[i][0]
   tmpY = pt[1] + connection[i][1]

   #Detect boundary points
   if tmpX < 0 or tmpY < 0 or tmpX >= gray.shape[0] or tmpY >= gray.shape[1]:
    continue

   if abs(int(gray[tmpX, tmpY]) - int(gray[pt])) < thresh and seedMark[tmpX, tmpY] == 0:
    seedMark[tmpX, tmpY] = 255
    seeds.append((tmpX, tmpY))
 return seedMark


path = "_rg.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Hist = CV2. Calchist ([gray], [0], none, [256], [0256]) histogram

seeds = originalSeed(gray, th=253)
seedMark = regionGrow(gray, seeds, thresh=3, p=8)

#plt.plot(hist)
#plt.xlim([0, 256])
#plt.show()
cv2.imshow("seedMark", seedMark)
cv2.waitKey(0)

The above example about the region growth of initial seed automatic selection (Python + openCV) is the whole content shared by Xiaobian. I hope to give you a reference, and I hope you can support developeppaer more.