简体   繁体   English

如何更有效地预处理以下图像?

[英]How can I preprocess following image more efficiently?

I am trying to preprocess images of cloth (ex. below我正在尝试预处理布料的图像(例如,下面

normal image )正常图像

and apply to preprocess it so it would look like this so stitches and rest of the fabric are easily distinguishable (ex. below)并对其进行预处理,使其看起来像这样,因此织物的针迹和 rest 很容易区分(例如下面)

binarised image二值化图像

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:

initial image初始图像

step 2:第2步:

grayscale灰度

step 3:第 3 步:

noise reduction降噪

step 4:第4步:

final binarised image最终二值化图像

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.

相关问题 如何才能更有效地编写此解决方案? - How can I write this solution more efficiently? 如何更高效地编写以下代码? - How to write the following code more efficiently? 预处理节点/边缘数据或重新格式化,以便 Gurobi 可以更有效地优化 - Preprocess node/edge data or reformat so Gurobi can optimize more efficiently 如何在Django中有效渲染图像? - How can I efficiently render an image in Django? 如何预处理我的图像,以便 SVM 可以像处理 MNIST 数据集一样处理它 - How can I preprocess my image so it can be processed by a SVM in the same way it processes the MNIST dataset 在预处理 function 后,如何保持图像和坐标(z 轴)之间的关联? - How can I keep the association between image and coordinate (z-axis) after a preprocess function? 如何为Tesseract预处理此图像? - How to preprocess this image for Tesseract? 如何更有效地在 Python 中压缩 jpegs? - How can I compress jpegs in Python more efficiently? 如何在更短的时间内更高效地生成数独 Python - How can I Generate Sudoku Python in less time and more efficiently 对于 ant 模拟,如何更有效地存储和使用值? - How can i store and use values more efficiently for a ant simulation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM