OpenCV-如何对RGB图像使用FloodFill?

问题描述 投票:2回答:2

我正在尝试在下面的图像上使用floodFill提取天空:

enter image description here

但是即使我设置了loDiff=Scalar(0,0,0)upDiff=Scalar(255,255,255),结果也只是显示种子点,并且不会变大(绿色点):

enter image description here

代码:

Mat flood;
Point seed = Point(180, 80);
flood = imread("D:/Project/data/1.jpeg");
cv::floodFill(flood, seed, Scalar(0, 0, 255), NULL, Scalar(0, 0, 0), Scalar(255, 255, 255));
circle(flood, seed, 2, Scalar(0, 255, 0), CV_FILLED, CV_AA);

这是结果(红色点是种子):

enter image description here

如何设置功能以获得更大的区域(例如整个天空)?

python c++ opencv image-processing flood-fill
2个回答
2
投票
请参见floodFill documentation

loDiff –当前观察到的像素与其所属组件的相邻像素之间或添加到该组件的种子像素之间的最大较低亮度/色差。upDiff –当前观察到的像素与属于该组件的相邻像素之一或添加到该组件的种子像素之间的最大上亮度/色差。

这里是Python代码示例:

import cv2 flood = cv2.imread("1.jpeg"); seed = (180, 80) cv2.floodFill(flood, None, seedPoint=seed, newVal=(0, 0, 255), loDiff=(5, 5, 5, 5), upDiff=(5, 5, 5, 5)) cv2.circle(flood, seed, 2, (0, 255, 0), cv2.FILLED, cv2.LINE_AA); cv2.imshow('flood', flood) cv2.waitKey(0) cv2.destroyAllWindows()

结果:floor

0
投票
例如clusters=3

输入图像-> Kmeans颜色分割

填充结果为绿色

注意,分割后,只有三种颜色定义了图像。这样,洪水将更好地沿着山脉/树木的轮廓]

代码

import cv2 import numpy as np # Kmeans color segmentation def kmeans_color_quantization(image, clusters=8, rounds=1): h, w = image.shape[:2] samples = np.zeros([h*w,3], dtype=np.float32) count = 0 for x in range(h): for y in range(w): samples[count] = image[x][y] count += 1 compactness, labels, centers = cv2.kmeans(samples, clusters, None, (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10000, 0.0001), rounds, cv2.KMEANS_RANDOM_CENTERS) centers = np.uint8(centers) res = centers[labels.flatten()] return res.reshape((image.shape)) # Load image and perform kmeans image = cv2.imread('1.jpg') kmeans = kmeans_color_quantization(image, clusters=3) result = kmeans.copy() # Floodfill seed_point = (150, 50) cv2.floodFill(result, None, seedPoint=seed_point, newVal=(36, 255, 12), loDiff=(0, 0, 0, 0), upDiff=(0, 0, 0, 0)) cv2.imshow('image', image) cv2.imshow('kmeans', kmeans) cv2.imshow('result', result) cv2.waitKey()

© www.soinside.com 2019 - 2024. All rights reserved.