我必须编写一个具有以下功能的函数:
fname
:具有多个位置的PNG文件colour
:元组(r,g,b)
fnameout
:另一个PNG文件该功能必须在PNG文件fname
中找到最大的色点(颜色由该函数指定),并保存一个只有最大色点的新文件(fnameout
),并以互补色为背景。我不知道如何找到地点。
输入图像:
期望的图像:
答案 0 :(得分:0)
您可以使用cv2
库中的findContours方法:
import cv2
import numpy as np
def find_biggest_spot(fname, colour, fnameout):
# load the image on disk and then display it
image = cv2.imread(fname)
height, width, channels = image.shape
grayScale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sigma = 0.33
v = np.median(grayScale)
low = int(max(0, (1.0 - sigma) * v))
high = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(grayScale, low, high)
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(x) for x in contours]
index = areas.index(max(areas))
output_image = np.zeros((height, width, 3), np.uint8)
cv2.drawContours(output_image, contours, index, colour[::-1], -1, cv2.LINE_AA, hierarchy, 0)
cv2.imwrite(fnameout, output_image)
find_biggest_spot('image.png', (255, 0, 0), 'result.png')
输入/输出图像: