can someone help me with dithering ? I'm trying to do random dithering, I want to do halftone and pattern too, but I'm stuck at random

25 views (last 30 days)
global selectedImage
imread(selectedIma);
X = imread(selectedImage);
%Val = zeros(numel(X)); % initialize pdf values storing variable with zeros
k = 1; % counter variable in for loop
% loop of (3 RGB colors)
for r = 1:size(X, 1) % for number of rows of the image
for c = 1:size(X, 2) % for number of columns of the image
if(getPixel(r,c)> rand*255)
setPixel(r,c, 255);
else
setPixel(r,c, 0);
k = k+1; % increment counter loop
end
end
end
axes(handles.axes9);
imshow(selectedImage);

Accepted Answer

DGM
DGM on 15 May 2022
Edited: DGM on 15 May 2022
I don't know what your setPixel() and getPixel() functions are, or why you're incrementing a counter variable that's apparently not used for anything. It's also unclear whether you're trying to process color images, since the comment suggests yes, but the code suggests no. If you're trying to incorporate color into a dithering process, there are trivial ways that generally produce poor results, and there are more significantly more complicated ways (that I don't have direct examples for).
You might want to start here. This discusses basic noise dithers, halftoning, ordered dithers and error diffusion dithers.
The code that Cris uses is largely MATLAB, but some of the tools are from DIPimage.
MIMT (also on the FEX) has functions for doing various dithers, including some of those listed above. Other than some input/output handling and avoidance of DIPimage and IPT dependencies, I don't recall that the core code is much different.
% a grayscale image
inpict = imread('lena.tif');
inpict = rgb2gray(inpict);
% use a small sample to reduce display interpolation effects
inpict = inpict(128:384,128:384);
% noise dithers
Dwhite = noisedither(inpict,'white');
Dblue = noisedither(inpict,'blue');
% basic ordered dither
Dordered = orddither(inpict);
% Zhou-Fang variable-coefficient error-diffusion dither
Derrdiff = zfdither(inpict);
Regarding color, the simple way is to just do the comparison on each of the three RGB channels independently. This results in an 8-color image that's generally pretty terrible. The only colors you get are black, white, and 6 primary and secondary colors. You can't apply a colormap or anything. This also kind of defeats the point of error diffusion. For an example of how the simple method is done, you can look at how orddither() does it (it's just a simple loop at the end).
Dordered = orddither(inpict,16,'color');
This isn't the method used by rgb2ind() with the 'dither' flag. That requires color quantization and it requires the index assignment to be incorporated into the error diffusion process. I don't have an example for that.
Due to a difference in intent and scope, the MIMT halftoning tools are more complicated (and cumbersome) than what Cris describes in the blog. You'd be better off just using his examples.
  6 Comments
DGM
DGM on 21 May 2022
This is just pulled straight out of orddither.m and turned into a script:
% a grayscale image
inpict = imread('lena.tif');
inpict = rgb2gray(inpict);
% use a small sample to reduce display interpolation effects
inpict = inpict(128:384,128:384);
% convert to unit-scale double
inpict = im2double(inpict);
sz = size(inpict);
numchans = size(inpict,3);
% generate the coefficient matrix
B = [8 3 4; 6 1 2; 7 5 9];
B = (B-1)/9; % normalize the coefficient matrix
% create mask by tiling the coefficient matrix
% so that it covers the same area as the image
n = size(B);
t = repmat(B,ceil(sz./n));
t = t(1:sz(1),1:sz(2));
% binarize image by comparing gray levels to tiled mask
out = inpict > t;
imshow(out)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!