特征提取并拍摄颜色直方图

时间:2018-09-03 17:14:11

标签: python opencv image-processing scikit-learn scikit-image

我正在研究图像处理特征提取。我有一张鸟的照片,我必须在其中提取鸟的面积,并说出鸟的颜色。我使用了精巧的特征提取方法来获取鸟的边缘。

如何仅提取鸟类区域并使背景变为蓝色?

openCv解决方案也应该没问题。

enter image description here

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)

enter image description here

from skimage import feature
edges = feature.canny(bird,sigma=1)
plt.imshow(edges )

enter image description here

可以从bird link拍摄实际鸟像

2 个答案:

答案 0 :(得分:22)

  1. Identify the edges图片Sobel edge map

  2. Binarize the image通过自动阈值binarized edge map

  3. 使用contour detection来识别黑色区域which are inside a white region并将其与白色区域合并。 (样机,图像可能会略有不同)Mockup of the merged mask

  4. 使用创建的图像作为遮罩为背景着色和着色 final image只需将每个背景像素(黑色)设置为其各自的颜色即可。

如您所见,该方法远非完美,但应为您提供有关如何完成任务的一般思路。可以通过稍微侵蚀地图以使其紧贴鸟的轮廓来改善最终图像质量。然后,您还可以通过仅考虑前景像素,使用遮罩来计算颜色直方图。 编辑:看这里:

  1. Eroded mask

eroded mask

  1. 最终图片

Final image with eroded mask

答案 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)