Home > Net >  Function to only get red channel for image
Function to only get red channel for image

Time:05-25

I'm trying to create a function which only show the red channels of an image. I've trialled a few different codes, and none of them seem to work. Below are a few examples that I've trialled thus far.

def justred(img, red):
red = img.imshow(im_r.astype(int))       
return img

this is the second code I've tried:

def justred(img, red):

if img != img.imshow(im[:, :, 0], cmap='Reds_r'):
    img.imshow(im[:, :, 0], cmap='Reds_r')
else:
    img.imshow(im[:, :, 0], cmap='Reds_r'
return img

The below image is the output I'm trying to achieve red channel images

CodePudding user response:

i wrote a function about masking images according to color. i edited it for you. it may help. i use OpenCV for image processing.

import cv2
import numpy as np
img=cv2.imread("L3ljT.png")
def red(img):
    
    img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    lower_red = np.array([0,50,50])
    upper_red = np.array([10,255,255])
    mask = cv2.inRange(img_hsv, lower_red, upper_red)
    
    
    lower_red = np.array([170,50,50])
    upper_red = np.array([180,255,255])
    mask1 = cv2.inRange(img_hsv, lower_red, upper_red)
    
    mask = mask mask1
    
    output_img = img.copy()
    output_img[np.where(mask==0)] = 0
    
    
    return output_img
    
output_img = red(img)
cv2.imshow("result",output_img)
if cv2.waitKey(0) == ord("q"):
    cv2.destroyAllWindows()

CodePudding user response:

A simple way of doing that is to just set the Green and Blue channels to zero, so if your image is in a (480,640,3) array of RGB channels called im:

# Copy image so we don't mess up the original
red = im.copy()
# Set Green and Blue channels to zero
red[..., [1,2]] = 0
  • Related