关于从8位二进制图像像素中提取MSB与LSB的技术实现咨询
Hey there! I see you're working on an image encryption project and have already nailed the pixelation and 8-bit conversion steps. Let's break down how to separate the Most Significant Bit (MSB) and Least Significant Bit (LSB) from each pixel value—this is super straightforward with bitwise operations, which are made for this exact kind of task.
Quick Bitwise Primer
First, a quick refresher to align:
- An 8-bit pixel value ranges from 0 to 255. Its MSB is the leftmost bit (7th position, counting from 0), which represents either 128 (if set to 1) or 0 (if set to 0).
- The LSB is the rightmost bit (0th position), representing either 1 or 0.
We can use two simple bitwise tricks to pull these out:
- MSB Extraction: Right-shift the pixel value by 7 bits (
value >> 7). This moves the MSB to the 0th position, leaving us with a decimal 0 or 1. - LSB Extraction: Use a bitwise AND with 1 (
value & 1). This masks all bits except the LSB, resulting in a decimal 0 or 1.
Modified Code with MSB/LSB Extraction
Here's how to slot this logic into your existing code. We'll work with the pixelated temp image (smaller size = faster processing) and create separate outputs for MSB and LSB data:
import cv2 import numpy as np # Taking input image input_img = cv2.imread('test.webp') cv2.imshow('Original Image', input_img) # Get input size and resize to pixelated size height, width = input_img.shape[:2] w, h = (256, 256) temp = cv2.resize(input_img, (w, h), interpolation=cv2.INTER_LINEAR) # Initialize arrays to store MSB and LSB for each channel msb_array = np.zeros_like(temp) lsb_array = np.zeros_like(temp) # Iterate over each pixel and each color channel for i in range(h): for j in range(w): # Grab B, G, R values of the current pixel b, g, r = temp[i, j] # Extract MSB for each channel msb_b = b >> 7 msb_g = g >> 7 msb_r = r >> 7 # Scale to 0-255 to make the bit visible as black/white msb_array[i, j] = [msb_b * 255, msb_g * 255, msb_r * 255] # Extract LSB for each channel lsb_b = b & 1 lsb_g = g & 1 lsb_r = r & 1 # Scale to 0-255 for visibility lsb_array[i, j] = [lsb_b * 255, lsb_g * 255, lsb_r * 255] # Resize MSB/LSB arrays back to original image size if needed msb_output = cv2.resize(msb_array, (width, height), interpolation=cv2.INTER_NEAREST) lsb_output = cv2.resize(lsb_array, (width, height), interpolation=cv2.INTER_NEAREST) # Save all outputs cv2.imwrite("pixelate.jpg", cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)) cv2.imwrite("msb_image.jpg", msb_output) cv2.imwrite("lsb_image.jpg", lsb_output) # Display results cv2.imshow('Pixelated Image', cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)) cv2.imshow('MSB Image', msb_output) cv2.imshow('LSB Image', lsb_output) cv2.waitKey(0) cv2.destroyAllWindows()
Key Details:
- We multiply the 0/1 bit values by 255 to turn them into visible black (0) or white (255) pixels. If you just need the raw 0/1 decimal values for encryption, skip this scaling step.
- OpenCV stores images in BGR format, so we process each color channel separately—critical if your encryption requires per-channel bit data.
- Using
np.zeros_like(temp)creates arrays that match the pixelated image's shape and data type, making it easy to store our results cleanly.
If you need to work with the raw 0/1 bit values directly (not scaled to image format), just remove the *255 operations and use msb_array/lsb_array directly in your encryption logic.
内容的提问来源于stack exchange,提问作者Muhammad Islam Kamran

