簡體   English   中英

Python - 找到每一列中的第一個和最后一個白色像素

[英]Python - Find the first white pixel and last one in each column

我怎樣才能在一列二值圖像中找到第一個和最后一個白色像素。 我計算圖像中點之間的距離

我做了,但一切仍未解決,請幫助我。 咨詢了一些方法,確實不盡如人意。

對於一列,你應該有類似 col = [[p0_r, p0_g, p0_b], ..., [pn_r, pn_g, pn_b]] 的東西來找到第一個和最后一個白色像素,你應該做類似的事情

first_white = col.index([0, 0, 0])
rev = col.copy()
rev.reverse() # reverse the column order
last_white = len(col) - rev.index([0, 0, 0]) - 1

你可能有這樣的經歷,希望對你有幫助

import cv2
import numpy as np

frame = cv2.imread("//Path/To/Image.png")

color_pixel = []

# Find White so RGB = [255, 255, 255]
rgb_color_to_find = [0xFF, 0xFF, 0xFF]
for i in range(frame.shape[1]):
    # Reverse color_to_find_rgb (2 -> 1 -> 0) cause
    # OpenCV use BGR and not RGB
    tmp_col = np.where(
        (frame[:, i, 0] == rgb_color_to_find[2])
        & (frame[:, i, 1] == rgb_color_to_find[1])
        & (frame[:, i, 2] == rgb_color_to_find[0])
    )

    # If minimum 2 points are find
    if len(tmp_col[0]) >= 2:
        first_pt = (i, tmp_col[0][0])
        last_pt = (i, tmp_col[0][-1])
        distance = tmp_col[0][-1] - tmp_col[0][0]
        color_pixel.append(((first_pt, last_pt), distance))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM