Вставка значений в пиксели в соответствии с кортежем - PullRequest
0 голосов
/ 02 марта 2019

В приведенном ниже коде я хочу найти все пиксели, которые являются черными после порогового значения, и преобразовать их в зеленый.

import cv2
import numpy as np
import os

# Two readings of the image - color and gray
color_img = cv2.imread('myimage.jpg', cv2.IMREAD_COLOR)

gray_img = cv2.imread('myimage.jpg', cv2.IMREAD_GRAYSCALE)

# Perform threshold
ret, thresh = cv2.threshold(gray_img, 50, 255, cv2.THRESH_BINARY)

# Get indices of black pixels
indices = np.where(thresh == [0])

color = color_img.copy()

color[indices[0][:]][indices[1][:]][:] = [0, 255, 0]

cv2.imwrite('greens.jpg', color)

По какой-то причине пиксели остаются в исходном значении.Почему значения остаются прежними?

1 Ответ

0 голосов
/ 03 марта 2019

Вы можете сделать это так:

#!/usr/bin/env python3

import cv2
import numpy as np

# Read image and make greyscale version - don't annoy disks by reading twice!
color = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
gray  = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)

# Perform threshold
ret, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)

# Set all pixels in "color" to green where threshold is zero
color[(thresh==0)] = [0, 255, 0]

# Save result
cv2.imwrite('greens.jpg', color)

Итак, если вы начнете с этого изображения:

enter image description here

Выполучит это как ваш результат:

enter image description here

...