[英]How can I preprocess following image more efficiently?
I am trying to preprocess images of cloth (ex. below我正在尝试预处理布料的图像(例如,下面
and apply to preprocess it so it would look like this so stitches and rest of the fabric are easily distinguishable (ex. below)并对其进行预处理,使其看起来像这样,因此织物的针迹和 rest 很容易区分(例如下面)
My end goal is to train a model on these preprocessed images to detect bad stitches.我的最终目标是在这些预处理图像上训练 model 以检测不良针迹。
The ideal preprocessing should be理想的预处理应该是
step 1:步骤1:
step 2:第2步:
step 3:第 3 步:
step 4:第4步:
Currently, I am using a hideous mish-mash of OpenCV functions to achieve the result I posted above;目前,我正在使用 OpenCV 函数的可怕混搭来实现我上面发布的结果; which is bad and not good enough as I want to create a live system.这很糟糕而且还不够好,因为我想创建一个实时系统。 I would like to hear your insights on how to optimise code below我想听听您对如何优化下面的代码的见解
I don't have enough reputation to post images that's why I've posted hyperlinks:)我没有足够的声誉来发布图片,这就是我发布超链接的原因:)
image = cv2.imread("C:/Users/name/Downloads/timepass/1.jpg")
image = cv2.resize(image, (800, 800))
thresh = 185
blur = cv2.bilateralFilter(image, 50, 175, 175)
blur = cv2.bilateralFilter(blur, 50, 175, 175)
im_bw = cv2.threshold(blur, thresh, 255, cv2.THRESH_BINARY)[1]
gray = cv2.cvtColor(im_bw, cv2.COLOR_BGR2GRAY)
final = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)[1]
cv2.imshow("bi", final)
I recommend that the first processing you apply is conversion to grayscale.我建议您应用的第一个处理是转换为灰度。 This will shorten the processing time, so that the following functions will only process one channel instead of three.这将缩短处理时间,因此以下函数将只处理一个通道而不是三个。
For noise removal, the bilateral filter is computationally expensive, its advantage is that it smoothes the image without losing edge information.对于去噪,双边滤波器的计算量很大,它的优点是可以平滑图像而不丢失边缘信息。 However, I would recommend a Gaussian filter, or averaging, with small kernels to avoid losing too much information.但是,我建议使用小内核的高斯滤波器或平均,以避免丢失太多信息。
In binarization, you can use an OTSU or adaptive threshold, they obtain dynamic thresholds according to the image.在二值化中,可以使用 OTSU 或自适应阈值,它们根据图像获得动态阈值。 Try them and see how it works.试试它们,看看它是如何工作的。 Then you apply a median filter to remove the impulsive noise, present in the binarized image.然后应用中值滤波器去除二值化图像中的脉冲噪声。 After that you use morphological operations, I recommend you to use aperture especially useful for noise removal.在你使用形态学操作之后,我建议你使用对噪声去除特别有用的光圈。 You can also play with dilation to increase the size of the stitches.您还可以使用扩张来增加针迹的大小。
I made a small program, you can include the morphological operations and add the otsu threshold.我做了一个小程序,可以包含形态学运算和添加大津阈值。 The function "remove_blobs_on_edges" as the name suggests removes those blobs that are on the edges.顾名思义,function“remove_blobs_on_edges”会删除边缘上的那些斑点。 The program is as follows:程序如下:
import numpy as np
import cv2
def remove_blobs_on_edges(image):
"""
Eliminates blobs on edges
"""
# use the binarized image as a mask for flood filling
activation = np.array(image, dtype=np.uint8)
mask_shape = (image.shape[0] + 2, image.shape[1] + 2)
ffill_mask = np.zeros(mask_shape, dtype=np.uint8)
# top and bottom rows
for i in range(0, activation.shape[0]):
for j in [0, activation.shape[1] - 1]:
if activation[i,j]:
cv2.floodFill(activation, ffill_mask, (j, i), 0)
# left and right columns
for i in [0, activation.shape[0] - 1]:
for j in range(0, activation.shape[1]):
if activation[i,j]:
cv2.floodFill(activation, ffill_mask, (j, i), 0)
return activation
image = cv2.imread('image2.png',0)
blured = cv2.blur(image,(3,3))
thresh = cv2.threshold(blured, 150, 255, cv2.THRESH_BINARY)[1]
thresh = remove_blobs_on_edges(thresh)
blured_mask = cv2.medianBlur(thresh,3)
before_after = np.hstack((thresh, blured_mask))
cv2.imshow("Result- Before/After",before_after)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.