特征提取并取颜色直方图

Feature extraction and take color histogram

我正在研究图像处理特征提取。我有一张鸟的照片,我必须在其中提取鸟的区域并告诉鸟有什么颜色。我使用精明的特征提取方法来获取鸟的边缘。

如何只提取鸟类区域并将背景设为蓝色?

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

  1. Identify the edges of your image

  2. Binarize the image via automatic thresholding

  3. 使用contour detection to identify black regions which are inside a white region and merge them with the white region. (Mockup, image may slightly vary)

  4. 使用创建的图像作为遮罩来给背景上色并上色 这可以通过简单地将每个背景像素(黑色)设置为其各自的颜色来完成。

如您所见,该方法远非完美,但应该能让您大致了解如何完成任务。可以通过稍微腐蚀地图以将其收紧到鸟类的轮廓来提高最终图像质量。然后,您还可以使用蒙版通过仅考虑前景像素来计算颜色直方图。 编辑:看这里:

  1. Eroded mask

  1. 最终图像

根据这篇文章 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)