Segment image and highlight object border using python

Asked

Viewed 2,905 times

0

I’m testing some algorithms to improve the image quality that my hardware is getting. I have in the image a pick and I would like to highlight the object contained in it.

imagem obtida

I used the code below to equalize the image, but I was not successful.

import cv2
import numpy as np

def histogram_equalize(img):
b, g, r = cv2.split(img)
red = cv2.equalizeHist(r)
green = cv2.equalizeHist(g)
blue = cv2.equalizeHist(b)
return cv2.merge((blue, green, red))
img = cv2.imread('pos20.jpg')

img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)

# equalize the histogram of the Y channel
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])

# convert the YUV image back to RGB format
img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)

cv2.imshow('Color input image', img)
cv2.imshow('Histogram equalized', img_output)
cv2.imwrite('20.jpg',img_output)

cv2.waitKey(0)

Output:

saida

what is the best algorithm to improve image quality and highlight the edge of the object?

  • 2

    But the result of equalization is just that, the problem there is the original image, try to equalize with the Numpy and probably the result will be very similar.

  • Your question has many problems, as I mentioned earlier. You don’t say exactly what is object, you don’t define what is "quality" in the context, you don’t say clearly your intention (just say you want to "highlight the edge of the object"). If you want to highlight the edge, why not use a border sensitive filter, such as a gradient or the famous Canny in place of equalization?

1 answer

1


Really this image "is bone", and your question even more, :-), but maybe applying something more related to segmentation, you can have better results.

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('img1.jpg',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in xrange(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

inserir a descrição da imagem aqui

  • Ready I used the TRUNC, but when saved the image ends up losing quality and looks gray scale only

  • 1

    To obtain good results in image treatment, always start at grayscale, in this case, when the image is loaded it is already converted to Grayscale. For segmentation the ideal is to test various algorithms. Try this lib

  • Good tip Sidon, will help me a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.