第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢測3種不同顏色的交集

如何檢測3種不同顏色的交集

ABOUTYOU 2022-05-24 10:37:02
我有一個由許多不同純色的多邊形創(chuàng)建的圖像。坐標本身沒有給出,但如果需要可以檢測。我正在尋找一種方法來檢測所有點,這些點是 3 種或更多不同顏色的交集。顏色是事先不知道的,可能彼此相似(例如,一種可能是 (255, 255, 250),另一種是 (255, 255, 245)。具體的顏色并不重要,只是事實是不同的)。例如,在下圖中,一個小星星標記了我正在尋找的所有點。
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經(jīng)驗 獲得超5個贊

由于您的注釋掩蓋了您要識別的交叉點,因此我制作了一個新的類似圖像。


http://img1.sycdn.imooc.com//628c44ef000177b203950395.jpg

我沒有試圖彎曲我的大腦來處理 8 位 RGB 顏色的 3 維,而是將其轉(zhuǎn)換為單個 24 位整數(shù),然后運行一個通用過濾器SciPy并計算每個 3x3 窗口中唯一顏色的數(shù)量并從中制作了一個新圖像。因此結(jié)果中的每個像素的亮度值等于其鄰域中的顏色數(shù)量。我通過將 Numpy 鄰居數(shù)組轉(zhuǎn)換為 Python 集合來計算顏色的數(shù)量——利用了集合中只能有唯一數(shù)字的事實。


#!/usr/bin/env python3


import numpy as np

from PIL import Image

from scipy.ndimage import generic_filter


# CountUnique

def CountUnique(P):

    """

    We receive P[0]..P[8] with the pixels in the 3x3 surrounding window, return count of unique values

    """

    return len(set(P))


# Open image and make into Numpy array

PILim = Image.open('patches.png').convert('RGB')

RGBim = np.array(PILim)


# Make a single channel 24-bit image rather than 3 channels of 8-bit each

RGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)


# Run generic filter counting unique colours in neighbourhood

result = generic_filter(RGB24, CountUnique, (3, 3))


# Save result

Image.fromarray(result.astype(np.uint8)).save('result.png')

http://img1.sycdn.imooc.com//628c44fc000119d303930397.jpg

此處顯示了生成的圖像,并拉伸了對比度,以便您可以在您尋找的交叉點看到最亮的像素。


結(jié)果圖像中值的直方圖顯示,有 21 個像素在其 3x3 鄰域中有 3 種唯一顏色,而 4,348 個像素在其鄰域中有 2 種唯一顏色。例如,您可以通過運行找到這些np.where(result==3)。


  Histogram:

    155631: (  1,  1,  1) #010101 gray(1)

      4348: (  2,  2,  2) #020202 gray(2)

        21: (  3,  3,  3) #030303 gray(3)

為了更有趣,我嘗試編寫@Micka 建議的方法,并給出相同的結(jié)果,代碼如下所示:


#!/usr/bin/env python3


import numpy as np

from PIL import Image

from skimage.morphology import dilation, disk


# Open image and make into Numpy array

PILim = Image.open('patches.png').convert('RGB')

RGBim = np.array(PILim)

h, w = RGBim.shape[0], RGBim.shape[1]


# Make a single channel 24-bit image rather than 3 channels of 8-bit each

RGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)


# Make list of unique colours

UniqueColours = np.unique(RGB24)


# Create result image

result = np.zeros((h,w),dtype=np.uint8)


# Make mask for any particular colour - same size as original image

mask = np.zeros((h,w), dtype=np.uint8)


# Make disk-shaped structuring element for morphology

selem = disk(1)


# Iterate over unique colours

for i,u in enumerate(UniqueColours):

   # Turn on all pixels matching this unique colour, turn off all others

   mask = np.where(RGB24==u,1,0)

   # Dilate (fatten) the mask by 1 pixel

   mask = dilation(mask,selem)

   # Add all activated pixels to result image

   result = result + mask


# Save result

Image.fromarray(result.astype(np.uint8)).save('result.png')

作為參考,我在命令行的 ImageMagick 中創(chuàng)建了禁用抗鋸齒的圖像,如下所示:


convert -size 400x400 xc:red -background red +antialias              \

  -fill blue   -draw "polygon 42,168 350,72 416,133 416,247 281,336" \

  -fill yellow -draw "polygon 271,11 396,127 346,154 77,86"          \

  -fill lime   -draw "polygon 366,260 366,400 120,400" patches.png

關(guān)鍵詞:Python、圖像、圖像處理、相交、相交、PIL/Pillow、鄰接、鄰里、鄰里、鄰居、鄰居、通用、SciPy、3x3、過濾器。


查看完整回答
反對 回復(fù) 2022-05-24
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號