How to change the gray level of a 8-bit gray level image to 7, 6, 5-bit gray level image using for loops?
50 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello all,
Below is my code. So if I perform the operations outside of the nested for loop (meaning as a simple script where I input each value individually) I get the correct values for the pixels in the outImage. However, when I try to compute it within the nested for loop, the program doesn't stop executing, and when I click pause, it doesn't throw an error, rather it opens a new script with some chunk of code.
Does anybody have any idea of what do? I thought this would work.
Best,
close all;
clear;
clc;
% read the input image as input image
inImage = imread('rose_copy.jpg');
% get the shape of the image
[row,col] = size(inImage);
outImage = zeros(row,col);
n = 6; % the number of bits for the grayscale. Say I want it from 8-bit to 6-bit in this case
for y = 1:1:row
for x = 1:1:col
pixel = uint8(inImage(y,x));
mask = uint8(256 - power(2,(8 - n)));
pixel2 = uint8(bitand(pixel,mask));
fin = bitsra(pixel2,8 - n);
outImage(y,x) = uint8(fin);
end
end
outImage = uint8(outImage);
imwrite(outImage,'Grayscale.jpg','jpg');
0 comentarios
Respuestas (1)
Image Analyst
el 26 de En. de 2021
To seqentially reduce the number of "used" bits in an 8 bit image you'd do
for k = 1 : 7
grayImage = grayImage / 2;
end
8 comentarios
Walter Roberson
el 28 de En. de 2021
binary is not a bad way to approach it. You would probably get more accurate answers than successive division by 2, because in MATLAB divivision of uint8 rounds
x = uint8(1:10);
N = 2;
dec2bin(x)
uint8(x)/2^N*2^N
uint8(round(double(x)/2^N)*2^N)
bitand(x, 255-2^N+1)
uint8(floor(double(x)/2^N)*2^N)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!