이미지에는 세 개의 채널 (R, G, B)이 있습니다.
이미지의 모든 흰색 블록은 세 채널 모두에서 255 픽셀을 갖기 때문에 흰색입니다.
흰색 블록을 빨간색으로 변환하려면 먼저 cv2를 사용하여 이미지 행렬을 읽은 다음 채널 1 ("G") 및 채널 2 ("B")의 값이 255 인 모든 픽셀이 0으로 변경됩니다 이제 채널 0 ("R")에서만 픽셀 값 255가됩니다. 이렇게하면 모든 흰색 블록의 이미지가 빨간색 블록으로 바뀝니다.
두 파일이 첨부 : 1. old_square.jpg 2. new_square.jpg new_square.jpg에 표시된 붉은 색으로 착색되는 백색 사각형이 old_square.jpg
.
확인 다음 스크립트 :하지만 난 테두리와 흰색 영역의 색상 변경을 원하는 그나마 @sansingh 답변에 대한
# libraries
import cv2
import numpy as np
import Image
# name of jpg image file
jpg_image_name = "old_square.jpg"
# reading jpg image and getting its matrix
jpg_image = cv2.imread(jpg_image_name)
jpg_image_mat = np.array(jpg_image)
# getting image features
pixel_value_to_replace = 255
rows, cols, channels = jpg_image_mat.shape
"""##########################################
An image have three channels (R, G, B). So,
putting 0 in other two channels to make image
red at white squares.
##########################################"""
# changing 255 to 0 in first channel
for i in range(rows):
for j in range(cols):
if(jpg_image_mat[i, j, 1] == pixel_value_to_replace):
jpg_image_mat[i, j, 1] = 0
# changing 255 to 0 in second channel
for i in range(rows):
for j in range(cols):
if(jpg_image_mat[i, j, 2] == pixel_value_to_replace):
jpg_image_mat[i, j, 2] = 0
# saving new modified matrix in image format
new_image = Image.fromarray(jpg_image_mat)
new_image.save("new_square.jpg")
old_square.jpg
new_square.jpg
감사 만 다른 색상보다는 착색한다 검은. –
귀하가 게시 한 이미지를 반드시 작업해야하는 이미지로 간주 할 수 있습니까? – sansingh
@ Dash_25 위의 스크립트에서 결과를 얻으려는 것처럼 동일한 논리로 작업 할 수 있습니다. – sansingh