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
12 views (last 30 days)
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
k = k+1; % increment counter loop
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.