Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python 3.x - segment non-homogeneous image background with opencv

I have an image of a scanner and i wanted to segment the background but the background is not homogeneous. how could i do that?

My Input is a cloud points

input

I used the code below to convert to a depth image

import numpy as np
import cv2 
import open3d as o3d
import sys

def convertToDepth():
    argv = sys.argv[1]
    argv2 = sys.argv[2]
    pcd = o3d.io.read_point_cloud(argv)
    p = np.asarray(pcd.points)
    points = np.copy(p)

    points = np.asarray(points, np.int16)
    max_x = np.max(points[:,0])
    min_x = np.min(points[:,0])
    max_y = np.max(points[:,1])
    min_y = np.min(points[:,1])

    img = np.zeros(((max_x - min_x)+1,(max_y-min_y)+1), np.uint8)
    img[img == 0] = 255
    max_z = np.max(points[:,2])
    min_z = np.min(points[:,2])
    for p in points:
        img[(p[0] - min_x),(p[1]-min_y)] = min(img[(p[0] - min_x),(p[1]-min_y)], int((p[2] - min_z)* (256/((max_z - min_z)+1))))
    cv2.imwrite(argv2,img)

convertToDepth()

this is my depth image

depth

I have tried this code to obtain the seeds

import cv2
import numpy as np

img = cv2.imread('amostra.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


ret,thresh = cv2.threshold(gray ,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('thresh.png',thresh)

and obtained this

output

How can i get the seeds with an non-homogeneous background?

UPDATE: I tried this code:

import cv2
import numpy as np
import sys

path_img = sys.argv[1]

img = cv2.imread(path_img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

for i in range(0,255):
    thresh = gray.copy()
    thresh[:] = 0
    thresh[(gray == i)] = 255
    num_labels, labels_im = cv2.connectedComponents(thresh)
    areaMaior = 100
    
    maskMaior = np.zeros((img.shape[0],img.shape[1]),np.uint8)
    for j in range(1,num_labels):
        mask = np.zeros((img.shape[0],img.shape[1]),np.uint8)
        mask[labels_im==j] = 255

        pixels = cv2.countNonZero(mask)
        if(pixels > areaMaior):
            maskMaior = mask
            areaMaior = pixels
    #cv2.imwrite('segment/areaimg'+str(i)+'.png', maskMaior)
    gray[(maskMaior==255)] = 255

cv2.imwrite('gray12.png',gray)


And obtained this

result

question from:https://stackoverflow.com/questions/65907849/segment-non-homogeneous-image-background-with-opencv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...