Counting white clusters horizontally in a processed fabric

*爱你&永不变心* 提交于 2020-08-08 05:37:43

问题


I processed a fabric material to obtain the image as shown below:

Original image:

Processed Image:

Now, I want to find the number white clusters in a row. If all the clusters are uniform and perfectly horizontal, I would have run a loop to count the raise and drop in intensities to find number of clusters ,but thats not the case.If I take median/mean of several rows by the above method, the required answer is differing by a huge margin.

Is there any way to count them accurately on the constraint that rows need not be perfectly horizontal?Or Would following any method makes the task simpler?


回答1:


I am still thinking about this further, but for the moment, it seems ImageMagick does a pretty good job of deskewing your image. Just in Terminal (or Command Prompt on Windows):

 convert fabric.jpg -deskew 50% result.jpg

Here is an animation of what happens if you rotate your image from -20 to +20 degrees and on the right-hand side I show the projection (row sum) of each row. Watch that rightmost column as the left-hand side becomes horizontal:




回答2:


I guess you can find out the number of connected components and get the number of labels as bellow. here bp8OO.jpg your grayscale image. Now I guess you can do something on this for finding out how many in each row.

import cv2
import numpy as np

img = cv2.imread('bp8OO.jpg', 0)
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]  # ensure binary
ret, labels = cv2.connectedComponents(img)

print("Number of labels" , len(labels))

def show_components(labels):
    # Map component labels to hue val
    label_hue = np.uint8(179*labels/np.max(labels))
    blank_ch = 255*np.ones_like(label_hue)
    labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

    # cvt to BGR for display
    labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

    # set bg label to black
    labeled_img[label_hue==0] = 0

    cv2.imshow('labeled.png', labeled_img)
    cv2.waitKey()

show_components(labels)



来源:https://stackoverflow.com/questions/56585981/counting-white-clusters-horizontally-in-a-processed-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!