我正在研究图像处理特征提取。我有一张鸟的照片,我必须在其中提取鸟的面积,并说出鸟的颜色。我使用了精巧的特征提取方法来获取鸟的边缘。
如何仅提取鸟类区域并使背景变为蓝色?
openCv解决方案也应该没问题。
import skimage
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import os
filename = os.path.join(os.getcwd(),'image\image_bird.jpeg')
from skimage import io
bird =io.imread(filename,as_grey=True)
plt.imshow(bird)
from skimage import feature
edges = feature.canny(bird,sigma=1)
plt.imshow(edges )
可以从bird link拍摄实际鸟像
答案 0 :(得分:22)
Binarize the image通过自动阈值
使用contour detection来识别黑色区域which are inside a white region并将其与白色区域合并。 (样机,图像可能会略有不同)
如您所见,该方法远非完美,但应为您提供有关如何完成任务的一般思路。可以通过稍微侵蚀地图以使其紧贴鸟的轮廓来改善最终图像质量。然后,您还可以通过仅考虑前景像素,使用遮罩来计算颜色直方图。 编辑:看这里:
答案 1 :(得分:7)
根据本文https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/ 还有这个问题CV - Extract differences between two images
我写了一些如下的python代码。正如我的前任所说,这还远非完美。该代码的主要缺点是必须手动设置常量值:minThres(50),maxThres(100),扩张迭代次数和侵蚀迭代次数。
import cv2
import numpy as np
windowName = "Edges"
pictureRaw = cv2.imread("bird.jpg")
## set to gray
pictureGray = cv2.cvtColor(pictureRaw, cv2.COLOR_BGR2GRAY)
## blur
pictureGaussian = cv2.GaussianBlur(pictureGray, (7,7), 0)
## canny edge detector - you must specify threshold values
pictureCanny = cv2.Canny(pictureGaussian, 50, 100)
## perform a series of erosions + dilations to remove any small regions of noise
pictureDilate = cv2.dilate(pictureCanny, None, iterations=20)
pictureErode = cv2.erode(pictureDilate, None, iterations=5)
## find the nozero regions in the erode
imask2 = pictureErode>0
## create a Mat like pictureRaw
canvas = np.full_like(pictureRaw, np.array([255,0,0]), dtype=np.uint8)
## set mask
canvas[imask2] = pictureRaw[imask2]
cv2.imwrite("result.png", canvas)