简体   繁体   English

如何清理这张图片(opencv-python)?

[英]How can I clean this picture up (opencv-python)?

I am really new to opencv.我对 opencv 真的很陌生。 How can I remove the noise in the background without losing info?如何在不丢失信息的情况下消除背景噪音?

I started with this: and Otsu thresholded it.我是从这个开始的:Otsu 对它进行了阈值化。 I've tried erosion, dilation, bilateral filtering.我试过腐蚀、膨胀、双边过滤。 My goal is to get a rectangle on the borders so I can perspective transform the thresholded picture, but it has trouble finding contours.我的目标是在边界上获得一个矩形,这样我就可以透视变换阈值图片,但它很难找到轮廓。 Or maybe is there a different and better approach?或者也许有不同的更好的方法?

Here is one way to do that in Python/OpenCV.这是在 Python/OpenCV 中执行此操作的一种方法。

  • Read the input读取输入
  • Blur it模糊它
  • Convert to HSV and extract the saturation channel转换为 HSV 并提取饱和通道
  • Threshold the saturation image阈值饱和图像
  • Clean it up with morphology close and open and save as a mask用形态关闭和打开将其清理并保存为蒙版
  • Recreate your OTSU threshold image重新创建您的 OTSU 阈值图像
  • Write black to OTSU image where mask is black (zero)将黑色写入掩码为黑色(零)的 OTSU 图像
  • For comparison, write black to Input image where mask is black (zero)为了比较,将黑色写入掩码为黑色(零)的输入图像
  • Save results保存结果

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# read image
img = cv2.imread('circuit_board.jpg')

# blur
blur = cv2.GaussianBlur(img, (3,3), 0)

# convert to hsv and get saturation channel
sat = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)[:,:,1]

# threshold saturation channel
thresh = cv2.threshold(sat, 50, 255, cv2.THRESH_BINARY)[1]

# apply morphology close and open to make mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
mask = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel, iterations=1)

# do OTSU threshold to get circuit image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# write black to otsu image where mask is black
otsu_result = otsu.copy()
otsu_result[mask==0] = 0

# write black to input image where mask is black
img_result = img.copy()
img_result[mask==0] = 0

# write result to disk
cv2.imwrite("circuit_board_mask.png", mask)
cv2.imwrite("circuit_board_otsu.png", otsu)
cv2.imwrite("circuit_board_otsu_result.png", otsu_result)
cv2.imwrite("circuit_board_img_result.png", img_result)


# display it
cv2.imshow("IMAGE", img)
cv2.imshow("SAT", sat)
cv2.imshow("MASK", mask)
cv2.imshow("OTSU", otsu)
cv2.imshow("OTSU_RESULT", otsu_result)
cv2.imshow("IMAGE_RESULT", img_result)
cv2.waitKey(0)


Mask image:蒙版图片:

在此处输入图像描述

OTSU threshold image: OTSU 阈值图像:

在此处输入图像描述

OTSU Result:大通结果:

在此处输入图像描述

Image Result:图像结果:

在此处输入图像描述

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

相关问题 如何使用 OpenCV-Python 更改某个区域的色调 - How can I change the hue of a certain area with OpenCV-Python 如何使用 opencv-python 在此图像上找到平行线? - How can I find parellel line on this image with opencv-python? 如何在opencv-python中的单独图像中选择ROI? - How can I select ROI in a separate image in opencv-python? 如何使用 OpenCV-Python 顺利检测焊接接头? - How can I smoothly detect the welding joint using OpenCV-Python? 如何计算两个摄像头之间的距离(opencv-python)? - How can I calculate the distance between two cameras (opencv-python)? 如何使用 opencv-python 使用霍夫线变换去除噪声 - How can I remove noise with Hough Line Transform with opencv-python 如何使用opencv-python从视网膜图像中检测视盘 - how can I detect optic disc from retinal image using opencv-python 如何使用 opencv-python 代码在树莓派 4b 上使用 MJPG 而不是 YUYV 录制视频 - How can I record a video with MJPG instead of YUYV on a raspberry PI 4b with opencv-python code 我如何在该网站上找到opencv-python 3.3.1的旧版本文件? - How can I find the old wheel file of opencv-python 3.3.1 on this website? 如何使用 OpenCV-Python 访问图像的像素? - How do I access the pixels of an image using OpenCV-Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM