删除图像中小于n大小(噪声)的像素-打开CV python [英] removing pixels less than n size(noise) in an image - open CV python

查看:108
本文介绍了删除图像中小于n大小(噪声)的像素-打开CV python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试减少图像中的噪点,并且当前正在运行此代码

i am trying to remove noise in an image less and am currently running this code

import numpy as np
import argparse
import cv2
from skimage import morphology

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
    help = "Path to the image")
args = vars(ap.parse_args())

# Load the image, convert it to grayscale, and blur it slightly
image = cv2.imread(args["image"])

cv2.imshow("Image", image)
cv2.imwrite("image.jpg", image)

greenLower = np.array([50, 100, 0], dtype = "uint8")
greenUpper = np.array([120, 255, 120], dtype = "uint8")

green = cv2.inRange(image, greenLower, greenUpper)
#green = cv2.GaussianBlur(green, (3, 3), 0)

cv2.imshow("green", green)
cv2.imwrite("green.jpg", green)

cleaned = morphology.remove_small_objects(green, min_size=64, connectivity=2)

cv2.imshow("cleaned", cleaned)
cv2.imwrite("cleaned.jpg", cleaned)



cv2.waitKey(0)

但是,尽管使用了remove_small_objects函数,但是图像似乎并未从绿色"变为清洁".为什么会这样,我如何清理图像?理想情况下,我只想隔离白菜的图像.

However, the image does not seem to have changed from "green" to "cleaned" despite using the remove_small_objects function. why is this and how do i clean the image up? Ideally i would like to isolate only the image of the cabbage.

我的思维过程是在阈值去除像素大小小于100的像素之后,然后使图像变得模糊并填充被白色包围的黑洞-这就是我在matlab中所做的.如果有人可以指导我获得与Matlab实现相同的结果,将不胜感激.感谢您的帮助.

My thought process is after thresholding to remove pixels less than 100 in size, then smoothen the image with blur and fill up the black holes surrounded by white - that is what i did in matlab. If anybody could direct me to get the same results as my matlab implementation, that would be greatly appreciated. Thanks for your help.

更改代码时犯了一些错误,已更新为当前版本并显示3张图片

made a few mistakes when changing the code, updated to what it currently is now and display the 3 images

图片:

绿色:

干净:

我的目标是从matlab实现中获得如下图所示的东西:

my goal is to get somthing like this picture below from matlab implementation:

推荐答案

预处理

在过滤图像时,一个好主意是使图像低通或使其模糊一点.这样,相邻像素的颜色会变得更均匀一些,因此可以减轻图像上较亮和较暗的斑点,并使孔洞不遮罩.

Preprocessing

A good idea when you're filtering an image is to lowpass the image or blur it a bit; that way neighboring pixels become a little more uniform in color, so it will ease brighter and darker spots on the image and keep holes out of your mask.

img = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(img, (15, 15), 2)
lower_green = np.array([50, 100, 0])
upper_green = np.array([120, 255, 120])
mask = cv2.inRange(blur, lower_green, upper_green)
masked_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('', masked_img)
cv2.waitKey()

当前,您正在尝试通过各种亮度不同的颜色包含图像-想要绿色像素,无论它们是暗还是亮.这在HSV色彩空间中更容易实现.在此处深入了解HSV色彩空间.

Currently, you're trying to contain an image by a range of colors with different brightness---you want green pixels, regardless of whether they are dark or light. This is much more easily accomplished in the HSV colorspace. Check out my answer here going in-depth on the HSV colorspace.

img = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(img, (15, 15), 2)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower_green = np.array([37, 0, 0])
upper_green = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
masked_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('', masked_img)
cv2.waitKey()

答案 > ngalstyan 展示了如何通过形态学很好地做到这一点.您想要做的就是称为 opening ,这是侵蚀的组合过程(该过程或多或少只是去除了一定半径内的所有物体),然后进行扩张(将其添加回任何剩余的物体,无论多少删除).在OpenCV中,这是通过 cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) .该页面上的教程显示了它的工作原理.

The answer provided by ngalstyan shows how to do this nicely with morphology. What you want to do is called opening, which is the combined process of eroding (which more or less just removes everything within a certain radius) and then dilating (which adds back to any remaining objects however much was removed). In OpenCV, this is accomplished with cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel). The tutorials on that page show how it works nicely.

img = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(img, (15, 15), 2)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower_green = np.array([37, 0, 0])
upper_green = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
opened_mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
masked_img = cv2.bitwise_and(img, img, mask=opened_mask)
cv2.imshow('', masked_img)
cv2.waitKey()

在上面,打开被显示为从二进制掩码中删除少量白色的方法. 关闭是相反的操作-从图像中删除被白色包围的黑色块.您可以使用与上述相同的方法来执行此操作,但可以使用

In the above, opening was shown as the method to remove small bits of white from your binary mask. Closing is the opposite operation---removing chunks of black from your image that are surrounded by white. You can do this with the same idea as above, but using cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel). This isn't even necessary after the above in your case, as the mask doesn't have any holes. But if it did, you could close them up with closing. You'll notice my opening step actually removed a small bit of the plant at the bottom. You could actually fill those gaps with closing first, and then opening to remove the spurious bits elsewhere, but it's probably not necessary for this image.

您可能想更轻松地在不同的色彩空间和阈值水平下玩耍,以感受最适合特定图像的效果.界面还不完善,但是有点麻烦,但是我有一个工具,您可以在线使用它在不同的色彩空间中尝试不同的阈值.如果需要,请此处进行检查.这就是我迅速找到您的图片的值的方式.

You might want to get more comfortable playing around with different colorspaces and threshold levels to get a feel for what will work best for a particular image. It's not complete yet and the interface is a bit wonky, but I have a tool you can use online to try out different thresholding values in different colorspaces; check it out here if you'd like. That's how I quickly found values for your image.

这篇关于删除图像中小于n大小(噪声)的像素-打开CV python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆