Preface:
Hello everyone, I am Dream. Today we will use HSV and YIQ color spaces to deal with image noise. In this experiment, we use any picture, addsalt and pepper noise
and convertback to RGB through the operation of
format, finally realizing the noise processing of image. Let’s take a look~RGB to HSV and YIQ
1. Import library function
First, we import the required libraries. Including numpy for processing array data, cv2 for image processing, and matplotlib for visualization.
import numpy as np import cv2 from matplotlib import pyplot as plt
2. Import the original image
Next, we import the raw image and convert it to RGB format for display.
img = cv2.imread('test.jpg') img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
3. Display the original image
Then, we display the raw RGB image using matplotlib.
plt.imshow(img) plt. title('Original RGB image') plt. show()
4. Convert RGB image to HSV and YIQ format
We use cvtColor function in cv2 to convert RGB image to HSV and YIQ format. COLOR_RGB2HSV and COLOR_RGB2YCrCb indicate conversion to the corresponding format.
img_hsv = cv2.cvtColor(img,cv2.COLOR_RGB2HSV) img_yiq = cv2.cvtColor(img,cv2.COLOR_RGB2YCrCb)
5. Add salt and pepper noise to the H channel of HSV
In the image in HSV format, we have selected the H channel. By randomly selecting a pixel, salt and pepper noise is added to the H channel of the pixel. The specific operation is to set the H value of the pixel to 255.
img_hsv_salt = img_hsv.copy() # Get image row number, column number and channel number information rows, cols, _ = img_hsv_salt.shape # Randomly select 100 pixels on the image and set its H channel value to 255 to simulate salt and pepper noise for i in range(100): x = np.random.randint(0, rows) y = np.random.randint(0, cols) # Set the H channel value of the selected pixel to 255 img_hsv_salt[x, y][0] = 255 img_hsv_salt = img_hsv. copy() # Get image row number, column number and channel number information rows, cols, _ = img_hsv_salt.shape # Randomly select 100 pixels on the image and set its H channel value to 255 to simulate salt and pepper noise for i in range(100): x = np.random.randint(0, rows) y = np.random.randint(0, cols) img_hsv_salt[x, y][0] = 255
6. Add salt and pepper noise to the Y channel of YIQ
In the image in YIQ format, we have selected the Y channel. In the same way, salt and pepper noise is added to the Y channel of the pixel by randomly selecting the pixel.
img_yiq_salt = img_yiq.copy() for i in range(100): x = np.random.randint(0,rows) y = np.random.randint(0,cols) img_yiq_salt[x,y][0] = 255
7. Display the H channel and Y channel with salt and pepper noise separately
Next, we display the H channels of the HSV and YIQ format images with salt and pepper noise added, respectively. Use the imshow function of matplotlib and set the display effect to grayscale.
plt.imshow(img_hsv_salt[:,:,0], cmap='gray') plt.title('Salt & amp; Pepper noise on H channel of HSV') plt. show() plt.imshow(img_yiq_salt[:,:,0], cmap='gray') plt.title('Salt & amp; Pepper noise on Y channel of YIQ') plt. show()
8. Synthesize HSV and YIQ format images with salt and pepper noise
We converted the images in HSV and YIQ formats with added salt and pepper noise back to RGB format for subsequent display.
img_hsv_salt = cv2.cvtColor(img_hsv_salt,cv2.COLOR_HSV2RGB) img_yiq_salt = cv2.cvtColor(img_yiq_salt,cv2.COLOR_YCrCb2RGB)
9. Display R, G, and B channels respectively
Next, we display the R, G, and B channels of the original RGB image separately. Use the imshow function of matplotlib and set the display effect to grayscale.
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(12, 4)) axs[0].imshow(img[:,:,0], cmap='gray') axs[0].set_title('R') axs[1].imshow(img[:,:,1], cmap='gray') axs[1].set_title('G') axs[2].imshow(img[:,:,2], cmap='gray') axs[2].set_title('B') plt. show()
10. Display the H, S, and V channels separately
Next, we display the H, S, and V channels of the HSV image with salt and pepper noise added, respectively. Among them, the H channel is displayed using the hsv color space, while the S and V channels are displayed using grayscale images.
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(12, 4)) axs[0].imshow(img_hsv[:,:,0], cmap='hsv') axs[0].set_title('H') axs[1].imshow(img_hsv[:,:,1], cmap='gray') axs[1].set_title('S') axs[2].imshow(img_hsv[:,:,2], cmap='gray') axs[2].set_title('V') plt. show()
11. Display HSV and YIQ format images with salt and pepper noise
Next, we use matplotlib to display images in HSV and YIQ format with salt and pepper noise added.
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 6)) axs[0].imshow(img_hsv_salt) axs[0].set_title('Salt & amp; Pepper noise on H channel of HSV') axs[1].imshow(img_yiq_salt) axs[1].set_title('Salt & amp; Pepper noise on Y channel of YIQ') plt. show()
12. Convert the synthesized HSV and YIQ format images with salt and pepper noise into RGB format and display
Finally, we converted the HSV and YIQ images with salt and pepper noise back into RGB format and displayed them using matplotlib.
img_hsv_salt_rgb = cv2.cvtColor(img_hsv_salt,cv2.COLOR_RGB2BGR) img_yiq_salt_rgb = cv2.cvtColor(img_yiq_salt,cv2.COLOR_RGB2BGR) plt.imshow(img_hsv_salt_rgb) plt.title('Salt & amp; Pepper noise on H channel of HSV RGB') plt. show() plt.imshow(img_yiq_salt_rgb) plt.title('Salt & amp; Pepper noise on Y channel of YIQ RGB') plt. show()
13. Summary
In this article, we used RGB to HSV and YIQ operations to noise the image by adding salt and pepper noise and converting it back to RGB format. We show the original RGB image and its R, G, B channel display, then convert the image to HSV and YIQ format
, and add salt and pepper noise in the H channel and Y channel respectively. Then, we displayed the H, S, V channel and Y channel with noise added
. Finally, we show the HSV and YIQ format images with salt and pepper noise added, and convert them back to RGB format
for display.
Through such operations, we can further understand the application of color space conversion in image processing, and how to simulate the actual scene in the image by adding noise. Additionally, we explored how to present the noise-processed image by converting back to RGB format. These techniques have important application value in image denoising, image enhancement and other related fields. These methods are of great significance for removing noise from images and improving image visual effects, and can play a role in many practical applications.
Recommendation in this issue:
Python from entry to proficiency (purchase channel)
IT Today's Hot List
: http://itoday.top/