How to remove area of interest from the background of an image?

3

I'm starting to work with image processing in pattern recognition, and I'm having to pre-render the images I'll use. First, I need to highlight the region of interest in the background of the image, can anyone tell me the best technique for this? I'm using opencv plus python for this procedure.

Second, I need to identify the colors of this region of interest and plot a 3d graph.

    
asked by anonymous 06.10.2018 / 13:50

1 answer

4

To extract you can use OpenCV Grab Cut >

A example can be seen in the "Interactive Foreground Extraction using GrabCut Algorithm" tutorial

Code

Using the example code:

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

img = cv2.imread('C:\Users\Desktop\teste\FHB9o.jpg')

mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (10,10,45,45)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
cv2.imshow('Imagem', img)
cv2.waitKey(0)
cv2.imwrite('C:\Users\Desktop\teste\resultado.jpg', img)
plt.imshow(img),plt.colorbar(),plt.show()

Result

Note

In this case, the rectangular ROI (Region of Interest) is fixed. If the images have variation in the location of the object that needs to be extracted, a dynamic ROI needs to be created, where the contours will be the points that will determine the coordinates of dynamic ROI.

    
08.10.2018 / 19:45