简体   繁体   English

锐化图像中每个 object 的边框像素

[英]sharpen out border pixels of each object in image

I have an image with some random components... border of each component has some blurred pixels ie...see screenshot我有一个带有一些随机组件的图像......每个组件的边框都有一些模糊的像素,即......看截图带有模糊像素的边界

So using OpenCv and python, i want this image to be really sharp...ie no blurry pixels on the edge... as shown in this image below所以使用 OpenCv 和 python,我希望这张图片非常清晰......即边缘没有模糊像素......如下图所示

在此处输入图像描述

Here is the full image for your refrence.....这是完整的图像供您参考...... 在此处输入图像描述

To a certain tolerance for errors this behaviour can be achived by using k-means algorithm.为了对错误有一定的容忍度,这种行为可以通过使用 k-means 算法来实现。 This algorithm can be used to build clusters of pixels having similar color.该算法可用于构建具有相似颜色的像素簇。 The OpenCV k-means implementation provides a lot of parameters to tweak with to get the desired results. OpenCV k-means实现提供了许多参数来调整以获得所需的结果。 You may use the following code snippet to start with:您可以使用以下代码片段开始:

import cv2
import numpy as np

img = cv2.imread("/path/to/img.png")
Z = img.reshape((-1, 3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

cv2.imwrite("./output.png", res2)

The above code will generate the following results for the sample image:上面的代码将为示例图像生成以下结果:

在此处输入图像描述

But this code may not work very will with the larger image(even after passing K=5), this is due to the fact that, we are depending upon the K-means to pick the seeds by random sampling.但是这段代码可能不适用于较大的图像(即使在通过 K=5 之后),这是因为我们依靠 K-means 通过随机采样来挑选种子。 We have an option of passing in the seed colors to look for which will be BGR values for yellow, light greem, dark green, brown and blue in your code.我们可以选择传入种子 colors 以查找代码中黄色、浅绿色、深绿色、棕色和蓝色的 BGR 值。 After supplying these BGR values as seed, you can get better results.将这些 BGR 值作为种子提供后,您可以获得更好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM