获取的像素位置

问题描述 投票:0回答:1

我有一个关于巨蟒和OpenCV的问题。我想改变图片的一部分是黑色的一些其它颜色(不管是什么)。改变后,我想获得的像素值,这8个标有红色圆圈。怎么做?

https://imgur.com/2E1Wwqg

import cv2
import numpy as np

img = cv2.imread("image.jpg");
img[np.where((img == [0,0,0]).all(axis = 2))] = [50,150,166]
cv2.imwrite('output.png', img)
cv2.imshow("shapes", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


python opencv
1个回答
1
投票

你可以做,使用OpenCV的findContours()minAreaRect()是这样的:

#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

# Theshold inverse so the black comes out white because findContours() looks for white objects
ret,thresh = cv2.threshold(imgray,16,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('thresh.png',thresh)

# Remove noise specks
thresh = cv2.medianBlur(thresh,5)
cv2.imwrite('thresh-m.png',thresh)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)

# Show user what we found
i=0
for cnt in contours:
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(im,[box],0,(255,0,0),2)
    print('Contour:{}\n{}'.format(i,box))
    i = i+1

cv2.imwrite('result.png',im)

该限图像看起来是这样的:

enter image description here

和结果图像看起来就像这样:

enter image description here

该程序输出是包含您的一条线路每一个的4个最小矩形的4个点。

Contour:0
[[416 776]
 [410 767]
 [659 607]
 [664 616]]
Contour:1
[[297 780]
 [ 77 599]
 [ 83 592]
 [303 773]]
Contour:2
[[518 695]
 [507 694]
 [519 176]
 [530 177]]
Contour:3
[[226 688]
 [224 174]
 [233 173]
 [235 687]]
© www.soinside.com 2019 - 2024. All rights reserved.