Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of OpenCV changing Pixel and modifying Image Channel

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you an example analysis of OpenCV changing pixels and modifying image channels. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Effect picture

The original image VS changes a pixel on the lower right to red, and changes the upper left corner 1x4 area to green. The effect image is as follows:

Cut the region of interest: capture the upper left corner, upper right corner, lower left corner and lower right corner, accounting for 1 / 4 of each corner. The effect image is as follows:

The effect of single-channel grayscale image of original VS image is as follows:

VS upper right R channel map VS lower left G channel map VS lower right B channel map is as follows:

Image 4-channel fully transparent image VS opaque effect picture:

two。 Source code # USAGE# python opencv_getting_setting.py-image fjdj.png# imports necessary package import argparseimport cv2import imutilsimport numpy as np# build command line parameters and parses #-image disk image path The default name is the current py file sibling directory: fjdj.jpgap = argparse.ArgumentParser () ap.add_argument ("- I", "- image", type=str, default= "fjdj.jpg", help= "path to the input image") args = vars (ap.parse_args ()) ap = argparse.ArgumentParser () # load image to get spatial dimensions (width, height) Show the original image to the screen image = cv2.imread (args ["image"]) image = imutils.resize (image, width=430) origin = image.copy () (h, w) = image.shape [: 2] cv2.imshow ("Original", image) # Image exists as Numpy array, get the upper left corner, image index starts from 0 # Image is represented by BGR channel, because at the beginning BGR is the standard Later adjusted to RGB (b, g, r) = image [0,0] print ("Pixel at (0,0)-Red: {}, Green: {}, Blue: {}" .format (r, g, b)) # to obtain the pixel value of the Xerox 380, the image is imagined as the matrix of M, and M is the row. N is the column (b, g, r) = image [380,380] print ("Pixel at (380,380)-Red: {}, Green: {}, Blue: {}" .format (r, g, b)) # Update xchang50 The pixels of Yellow20 are red image [380,380] = (0,0,0,255) (b, g, r) = image [380,380] print ("Pixel at (380,380)-Red: {}, Green: {}, Blue: {}" .format (r, g, b)) # calculate the center of the image (cX, cY) = (w / 2, h / 2) # use array slices to get the part tl = image [0:cY] of the upper left corner 0:cX] cv2.imshow ("Top-Left Corner", tl) # the same Cut the upper right corner, lower left corner and lower right corner with array slices And show tr = image [0:cY, cX:w] br = image [cY:h, cX:w] bl = image [cY:h, 0:cX] cv2.imshow ("Top-Right Corner", tr) cv2.imshow ("Bottom-Right Corner", br) cv2.imshow ("Bottom-Left Corner", bl) # use pixel slices to change the color of the pixel area image [0:cY, 0:cX] = (0,255) 0) # display the updated pixel image cv2.imshow ("Updated (Top-Left Corner to Green)", image) gray = cv2.cvtColor (origin, cv2.COLOR_BGR2GRAY) cv2.imshow ("Gray", gray) (h, w) = origin.shape [: 2] zeros = np.zeros ((h, w), dtype= "uint8") # separate origin into red Green and blue channels Then we use the Numpy zero array to construct the representation of each channel (B, G, R) = cv2.split (origin) R = cv2.merge ([zeros, zeros, R]) G = cv2.merge ([zeros, G, zeros]) B = cv2.merge ([B, zeros, zeros]) cv2.imshow ("B G R", np.hstack ([B, G) R]) # the original output frame is in the upper left corner of the red channel, the upper right corner of the green channel, the lower right corner of the green channel, the lower left corner of the blue channel output = np.zeros ((h * 2, w * 2,3), dtype= "uint8") output [0dtype= h, 0dtype= w] = originoutput [0dtype= h, w * 2] = Routput [h * 2, w * 2] = Bcv2.imshow ("origin vs R vs G vs B") Imutils.resize (output, width=700) alpha0 = np.dstack ([origin, np.ones ((h, w), dtype= "uint8") * 0]) cv2.imshow ("alph 0", alpha0) cv2.imwrite ("alph 0.png", alpha0) alpha1 = np.dstack ([origin, np.ones ((h, w), dtype= "uint8") * 255]) cv2.imshow ("alph 255", alpha1) cv2.imwrite (" alph 255.png ") Alpha1) cv2.waitKey (0) the above is an example analysis of OpenCV changing pixels and modifying image channels. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report