I am trying to implement Floyd Steinberg Dithering in MATLAB, using the pseudocode on the Wikipedia page https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering
My code is below
image = double(imread("dithering.jpg")) ./ 255;
levels = 2;
image_quantised = round(image .* (levels - 1)) ./ (levels - 1);
error = image - image_quantised;
height = size(image(:, :, 1), 1);
width = size(image(:, :, 1), 2);
image_dithered = image_quantised;
for y = 1:height - 1
for x = 2:width - 1
image_dithered(y , x + 1, :) = image_dithered(y , x + 1, :) + error(y, x, :) .* 7 / 16;
image_dithered(y + 1, x - 1, :) = image_dithered(y + 1, x - 1, :) + error(y, x, :) .* 3 / 16;
image_dithered(y + 1, x , :) = image_dithered(y + 1, x , :) + error(y, x, :) .* 5 / 16;
image_dithered(y + 1, x + 1, :) = image_dithered(y + 1, x + 1, :) + error(y, x, :) .* 1 / 16;
end
end
imshow(image_dithered) % Image 1
imshow(dither(mean(image, 3))) % Image 2
Image 1
Image 2
I am expecting the result in Image 2, but I am getting Image 1. It looks as though the algorithm isn't doing anything. Any ideas? :)
Edit: I have tried initialising image_dithered with different values; all zeros, the quantised image, and the original image. None of them work correctly
Edit 2: I'm getting closer by now calculating the error and quantisation within the loop. Still not spot on however.
for y = 1:height - 1
for x = 2:width - 1
new_pixel = round(image_dithered(y, x, :) .* (levels - 1)) ./ (levels - 1);
image_dithered(y, x, :) = new_pixel;
error = image(y, x, :) - new_pixel;
image_dithered(y , x + 1, :) = image_dithered(y , x + 1, :) + error .* 7 / 16;
image_dithered(y + 1, x - 1, :) = image_dithered(y + 1, x - 1, :) + error .* 3 / 16;
image_dithered(y + 1, x , :) = image_dithered(y + 1, x , :) + error .* 5 / 16;
image_dithered(y + 1, x + 1, :) = image_dithered(y + 1, x + 1, :) + error .* 1 / 16;
end
end
Edit 3: Thanks for the #saastn and #Cris Luengo, both of the answers helped me work out where I was going wrong and it appears to be working as expected now!
The fixed code is below for completeness.
height = size(image(:, :, 1), 1);
width = size(image(:, :, 1), 2);
image_dithered = image;
for y = 1:height - 1
for x = 2:width - 1
old_pixel = image_dithered(y, x, :);
new_pixel = round(image_dithered(y, x, :) .* (levels - 1)) ./ (levels - 1);
image_dithered(y, x, :) = new_pixel;
error = old_pixel - new_pixel;
image_dithered(y , x + 1, :) = image_dithered(y , x + 1, :) + error .* 7 / 16;
image_dithered(y + 1, x - 1, :) = image_dithered(y + 1, x - 1, :) + error .* 3 / 16;
image_dithered(y + 1, x , :) = image_dithered(y + 1, x , :) + error .* 5 / 16;
image_dithered(y + 1, x + 1, :) = image_dithered(y + 1, x + 1, :) + error .* 1 / 16;
end
end
imshow(image_dithered)
imshow(dither(mean(image, 3)))
The problem is that you tried to improve the pseudocode and remove oldpixel! Note that the algorithm does not calculate an error between the quantized pixel and its corresponding value in the original image, but rather the error between the quantized pixel and the previous value in the dithered image, which may have been updated while scanning the previous pixels.
Bring oldpixel back and review the whole algorithm one more time.
But even after the modifications, you can not expect the results to match the MATLAB output, which could be the result of differences in the details of the two implementations.
You’re propagating errors in image_quantized instead of in the original image. Remove this quantized image, it is not part of the algorithm.
You need to quantize one pixel, then find the difference with the original value, and propagate that error into future pixels.
Note that Wikipedia pseudocode does this in-place, there is only one copy of the image that works as both input and output.
On this old blog post of mine has MATLAB code for Floyd-Steinberg dithering.
Assuming you first follow saastn's answer (and replying to what it says about the differences between those images) : I'd say that by just looking at them in saastn's image, Matlab's patterns look like they're a sideways version of Floyd-Steinberg here, either by rotation, or more probably by transposition (swap x with y, which is a reflexion across the x+y diagonal axis). This can't be imitated by just changing coefficients, the image has to be processed by columns ("for x" outside of "for y", etc).
Related
I am trying to solve those four equations for "p, x, y, z," , which contains two parameters "b", "a", and I have used solve command as shown in the code:
clc,clear;
syms p x y z b a
eqn1 = (1)*p + (exp(0.5*b^(1/3)))*x + (exp(-0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y + (exp(-0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn2 = (1)*p + (exp(-0.5*b^(1/3)))*x + (exp(0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y - (exp(0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
eqn4 = (b^(1/3)*exp(-0.5*b^(1/3))-a*b^(2/3)*exp(-0.5*b^(1/3)))*x + (.866*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(.25*b^.333) - a(-.5*b^.666*cos(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*sin(.433*b^.333)*exp(.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(.25*b^.333)+.5*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-a(.5*b^.666*sin(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(.25*b^.333) ))*z == 0;
sol = solve([eqn1, eqn2, eqn3, eqn4 ], [p, x, y, z]);
pSol = sol.p
xSol = sol.x
ySol = sol.y
zSol = sol.z
, but it keeps showing errors, would you please suggest any solution to this issue.
Let's look at this :
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
I pick one of the wrong parts out, 4 in total.
a(-.5*b^.666
You see, if a(index) exists, index must be a positive integer. However, in your code the value of -.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333) might not be a positive integer. And A may not even be a matrix, let alone an index.
Plus, before you post your question, you' d better check the basic grammar issues to save each other's time.
OK, we have an image and (0,1,2,3,4,5,6,7) - as images, we need to show image that we have got, next right, the image that got redrawn by numbers (0,1,2,3,4,5,6,7), third on right, is pallete (0,1,2,3,4,5,6,7) and boxes with color.
a) Upload JPEG image. Choose amount rows M and amount columns N for drawing grid. Change image size for required size of grid.
b) Convert image .JPEG in indexed image with 8 colors and show it up as on picture.
c) Prepare image that shows color card.
d) Prepare image that shows grid and color numbers.
m = 80;
n = 60;
im = imresize(imread('1задание.jpg'), [m n] * 10, 'nearest');
small_im = imresize(im, [m n], 'nearest');
[X, map] = rgb2ind(small_im, 8);
big_small_im = im2uint8(ind2rgb(imresize(X, [m n] * 10, 'nearest'), map));
figure;
imshow([im ones(m * 10, 50, 3) * 255 big_small_im ones(m * 10, 50, 3) * 255 ...
generate_cool_map(map, m * 10)]);
digits = [];
for i = 0 : 7
digit = imread([int2str(i) '.png']);
digits = [digits digit];
end
pixel_s = 43;
final_im = im2uint8(ones(m * pixel_s, n * pixel_s, 3) * 255);
for i = 1 : m
for j = 1 : n
final_im((i - 1) * pixel_s + 1 : i * pixel_s, j * pixel_s, :) = zeros(pixel_s, 1, 3);
final_im(j * pixel_s, (j - 1) * pixel_s + 1 : j * pixel_s, :) = zeros(pixel_s, 1, 3);
final_im((i - 1) * pixel_s + 2 : (i - 1) * pixel_s + 2 + 39+4, (j - 1) * pixel_s + 2 : (j - 1) * pixel_s + 2 + 26, :) = digits(:, X(i, j) * 27 + 1 : (X(i, j) + 1) * 27, :);
end
end
figure;
imshow(final_im);
function res = generate_cool_map(map, s)
color_size = floor(s / 8);
m_map = zeros(8, 1, 3);
for i = 1 : 8
m_map(i, 1, :) = map(i, :);
end
res = imresize(m_map, [s, color_size], 'nearest');
res(:, 1:2, :) = zeros(s, 2, 3);
res(:, color_size - 1 : color_size, :) = zeros(s, 2, 3);
for i = 0 : 7
res(i * color_size + 1 : i * color_size + 2, :, :) = zeros(2, color_size, 3);
res(i * color_size + 1 + floor(color_size / 2) : i * color_size + 2 + ...
floor(color_size / 2),1:7,:) = zeros(2,7,3);
res(i * color_size + 1 + floor(color_size / 2) : i * color_size + 2 + ...
floor(color_size / 2), color_size - 6 : color_size, :) = zeros(2,7,3);
end
res(s - 1 : s, :, :) = zeros(2, color_size, 3);
res = [res ones(s, floor(color_size / 3), 3) * 255];
digits = [];
for i = 0 : 7
digit = imread([int2str(i) '.png']);
digits = [digits digit];
end
res = im2uint8(res);
for i = 0 : 7
res(i * color_size + floor(color_size / 3) : i * color_size + ...
floor(color_size / 3) + 39+4, color_size + 6 : color_size + 6 + 26, :) ...
= digits(:, i * 27 + 1 : (i + 1) * 27, :);
end
end
Task how shall look first figure:
I attempted but it seems here is some mistake. :c
Task how shall look second figure:
Figure 1: Resizing and Reducing Unique Colour Count
With the current amount of implementation details here is what I came up with. Resizing the image can be done using the imresize() function and 'nearest' neighbour interpolation. To reduce the number of colours the imapprox() function is used to limit the number of unique colours to 8. Reconstruction of the new image can be done using New_Image_Data (the colour card keys) and New_Colour_Map (the colour card values).
m = 50;
n = 60;
Original_Image = imread('peppers.png');
subplot(1,2,1); imshow(Original_Image);
title("Original Image");
Number_Of_Rows = m;
Number_Of_Columns = n;
%Resizing image%
Resized_Image = imresize(Original_Image,[m n],'nearest');
%Reducing the amount of colours%
Maximum_Intensity = 255;
[Image_Data,Colour_Map] = rgb2ind(Resized_Image,Maximum_Intensity);
[New_Image_Data,New_Colour_Map] = imapprox(Image_Data,Colour_Map,8);
subplot(1,2,2); imshow(New_Image_Data,New_Colour_Map);
title("Resized Image with 8 Colours");
Colour_Bar = colorbar;
set(Colour_Bar,'YTick',(0:7));
Figure 2: Plotting the Grid of Values
%Plotting the grid of card colour values%
Figure_2 = figure(2);
clf;
Figure_2.Position = [0 0 700 700];
x = (0:Number_Of_Rows);
y = (0:Number_Of_Columns);
[X,Y] = meshgrid(x,y);
Z = ones(length(x),length(y)).';
mesh(Y,X,Z);
axis off
hold on
view(0,90);
Image_Data = flip(New_Image_Data);
title("Colour Card Values/Keys");
for Row_Index = 1: Number_Of_Rows
for Column_Index = 1: Number_Of_Columns
text(Column_Index-0.5,Row_Index-0.5,num2str(Image_Data(Row_Index,Column_Index)));
end
end
Ran using MATLAB R2019b
I try to use finite element to solve 2D diffusion equation:
numx = 101; % number of grid points in x
numy = 101;
numt = 1001; % number of time steps to be iterated over
dx = 1/(numx - 1);
dy = 1/(numy - 1);
dt = 1/(10*(numt - 1));
x = 0:dx:1; %vector of x values, to be used for plotting
y = 0:dy:1; %vector of y values, to be used for plotting
P = zeros(numx,numy,numt); %initialize everything to zero
% Initial Condition
mu = 0.5;
sigma = 0.05;
for i=2:numx-1
for j=2:numy-1
P(i,j,1) = exp(-( (x(i)-mu)^2/(2*sigma^2) + (y(j)-mu)^2/(2*sigma^2))/(2*sigma^2)) / sqrt(2*pi*sigma^2);
end
end
% Diffusion Equation
for k=1:numt
for i=2:numx-1
for j=2:numy-1
P(i,j,k+1) = P(i,j,k) + .5*(dt/dx^2)*( P(i+1,j,k) - 2*P(i,j,k) + P(i-1,j,k) ) + .5*(dt/dy^2)*( P(i,j+1,k) - 2*P(i,j,k) + P(i,j-1,k) );
end
end
end
figure(1)
surf(P(:,:,30))
The initial condition of P is Gaussian distributed.
However, when increasing k (k=30 in my code), P blows up. However, P should decrease since it is a solution of diffusion equation.
I have no idea about the problem in P(i,j,k+1), how to fix this problem?
Thanks
The value of P(1, :, :), P(101, :, :), P(:, 1, :) and P(:, 101 , :) is always zero in your code. It means P is always zero at the boundary of the 2-D box. It doesn't obey the diffusion law.
First, you should give an initial condition for the boundary.
% Initial Condition
mu = 0.5;
sigma = 0.05;
for i=1:numx
for j=1:numy
P(i,j,1) = exp(-( (x(i)-mu)^2/(2*sigma^2) + (y(j)-mu)^2/(2*sigma^2))/(2*sigma^2)) / sqrt(2*pi*sigma^2);
end
end
And then calculate the value of P at the boundary in the diffusion process.
% Diffusion Equation
for k=1:numt
for i=2:numx-1
for j=2:numy-1
P(i,j,k+1) = P(i,j,k) + .5*(dt/dx^2)*( P(i+1,j,k) - 2*P(i,j,k) + P(i-1,j,k) ) + .5*(dt/dy^2)*( P(i,j+1,k) - 2*P(i,j,k) + P(i,j-1,k) );
end
end
% calculate the value of P at the boudary
P(1, :, k + 1) = 2 * P(2, :, k + 1) - P(3, : , K + 1);
P(numx, :, k + 1) = 2 * P(numx - 1, :, k + 1) - P(numx - 2, : , K + 1);
P(:, 1, k + 1) = 2 * P(:, 2, k + 1) - P(:, 3 , K + 1);
P(:, numy, k + 1) = 2 * P(:, numy - 1, k + 1) - P(:, numy - 2 , K + 1);
end
I want to write a program which will calculate the statistic in Bertrand paradox.
In my way, I want select two dot in circle , and pass line using them (two dots), it is my chord. Then I want to calculate how many of these chords are longer than sqrt(3); but when I run this script some of the chords are bigger than 2 ! ( radius of my circle is 1 )
I don't know what is wrong with it, can anybody help me?
See this link please for the formula used.
r1 = rand(1,1000000);
teta1 = 2*pi * rand(1,1000000);
x1 = r1 .* (cos(teta1));
y1 = r1 .* (sin(teta1));
r2 = rand(1,1000000);
teta2 = 2*pi * rand(1,1000000);
x2 = r2 .* (cos(teta2));
y2 = r2 .* (sin(teta2));
%solve this equation : solve('(t*x2 +(1-t)*x1)^2 +(t*y2 +(1-t)*y1)^2 =1', 't');
t1= ((- x1.^2.*y2.^2 + x1.^2 + 2*x1.*x2.*y1.*y2 - 2*x1.*x2 - x2.^2.*y1.^2 + x2.^2 + y1.^2 - 2*y1.*y2 + y2.^2).^(1/2) - x1.*x2 - y1.*y2 + x1.^2 + y1.^2)/(x1.^2 - 2*x1.*x2 + x2.^2 + y1.^2 - 2*y1.*y2 + y2.^2);
t2= -((- x1.^2.*y2.^2 + x1.^2 + 2*x1.*x2.*y1.*y2 - 2*x1.*x2 - x2.^2.*y1.^2 + x2.^2 + y1.^2 - 2*y1.*y2 + y2.^2).^(1/2) + x1.*x2 + y1.*y2 - x1.^2 - y1.^2)/(x1.^2 - 2*x1.*x2 + x2.^2 + y1.^2 - 2*y1.*y2 + y2.^2);
length = abs (t1-t2) * sqrt (( x2-x1).^2 + (y2-y1).^2);
hist(length)
flag = 0;
for check = length
if( check > sqrt(3) )
flag = flag + 1;
end
end
prob = (flag/1000000)^2;
Your formula for length is probably to blame for the nonsensical results, and given its length, it is easier to replace it than to debug. Here is another way to find the length of chord passing through two points (x1,y1) and (x2,y2):
Find the distance of the chord from the center
Use the Pythagorean theorem to find its length
In Matlab code, this is done by
distance = abs(x1.*y2-x2.*y1)./sqrt((x2-x1).^2+(y2-y1).^2);
length = 2*sqrt(1-distance.^2);
The formula for distance involves abs(x1.*y2-x2.*y1), which is twice the area of the triangle with vertices (0,0), (x1,y1), and (x2,y2). Dividing this quantity by the base of triangle, sqrt((x2-x1).^2+(y2-y1).^2), yields its height.
Also, putting 1000000 samples into mere 10 bins is a waste of information: you get a crude histogram for all that effort. Better to use hist(length,100).
Finally, your method of selecting two points through which to pass a line does not take them from the uniform distribution on the disk. If you want uniform distribution over the disk, use
r1 = sqrt(rand(1,1000000));
r2 = sqrt(rand(1,1000000));
because for a uniformly distributed point, the square of the distance to the center is uniformly distributed in [0,1].
Finally, I've no idea why you square in prob = (flag/1000000)^2.
Here is your code with aforementioned modifications.
r1 = sqrt(rand(1,1000000));
teta1 = 2*pi * rand(1,1000000);
x1 = r1 .* (cos(teta1));
y1 = r1 .* (sin(teta1));
r2 = sqrt(rand(1,1000000));
teta2 = 2*pi * rand(1,1000000);
x2 = r2 .* (cos(teta2));
y2 = r2 .* (sin(teta2));
distance = abs(x1.*y2-x2.*y1)./sqrt((x2-x1).^2+(y2-y1).^2);
length = 2*sqrt(1-distance.^2);
hist(length,100)
flag = 0;
for check = length
if( check > sqrt(3) )
flag = flag + 1;
end
end
prob = flag/1000000;
I'm trying to get into the field of computer vision, and to start I implemented a Sobel filter in MATLAB, which I read about here: http://en.wikipedia.org/wiki/Sobel_operator
Here is the code:
image = double(image);
kernelx = [ -1, 0, 1;
-2, 0, 2;
-1, 0, 1];
kernely = [ 1, 2, 1;
0, 0, 0;
-1, 0, 1];
height = size(image,1);
width = size(image,2);
channel = size(image,3);
for i = 2:height - 1
for j = 2:width - 1
for k = 1:channel
magx = 0;
magy = 0;
for a = 1:3
for b = 1:3
magx = magx + (kernelx(a, b) * image(i + a - 2, j + b - 2, k));
magy = magy + (kernely(a, b) * image(i + a - 2, j + b - 2, k));
end;
end;
edges(i,j,k) = sqrt(magx^2 + magy^2);
end;
end;
end;
Here is an image I tested it on:
This is the result:
I don't know where to go from here, I've tried looking at line thinning or thresholding, what steps should I take to make this run better?
Your kernel in the y direction seems to be incorrect, it should be
[ 1, 2, 1;
0, 0, 0;
-1, -2, -1];
Further, if you want to improve edge detection, you can look into Hysteresis, its an easy way to complete some obvious contours in an image which might be missed out
http://en.wikipedia.org/wiki/Canny_edge_detector#Tracing_edges_through_the_image_and_hysteresis_thresholding
Results first
1 Typo in Kernel
As Bharat Singh pointed out, your y-Kernel looks wrong. (Later analysis shows that it changes the results but that isn't the main problem.) If you want you can use your original kernel in my code below to see what the result is. (For posterity: kernely(3,:) = [-1, 0, 1];) Basically, it looks like the input image.
2 Use Convolution to make debugging fast
Before you do anything else, just use the convolution function that is provided by Matlab.
Also, to speed things up, use conv2. While you are experimenting, you might want to use parfor instead of the outer for-loop. The convolution is instantaneous on my machine and the parfor version takes minutes.
3 imshow is causing problems
There is an alternative named imagesc, that scales the image automatically. Or you can call imshow(image, []). But the problem in this case is that each channel looks right but the automated mixing of the channels by Matlab doesn't work. (Remember they are not RGB anymore but more like magnitude of the R-channel derivative |dR| etc. )
Check this by looking at each resulting channel individually (imshow(E(:,:,1), [])) or as a euclidean average (see my code). If you run imshow(E, []) you get the blown out highlights. The same happens if you pass it through rgb2gray first.
Code
image = imread('cat.jpg');
image = double(image);
kernelx = [ -1, 0, 1;
-2, 0, 2;
-1, 0, 1];
kernely = [ 1, 2, 1;
0, 0, 0;
-1, -2, -1];
height = size(image,1);
width = size(image,2);
channel = size(image,3);
edges = zeros(height, width, channel);
if exist('chooseSlow', 'var')
parfor i = 2:height - 1
for j = 2:width - 1
for k = 1:channel
magx = 0;
magy = 0;
for a = 1:3
for b = 1:3
magx = magx + (kernelx(a, b) * image(i + a - 2, j + b - 2, k));
magy = magy + (kernely(a, b) * image(i + a - 2, j + b - 2, k));
end;
end;
edges(i,j,k) = sqrt(magx^2 + magy^2);
end;
end;
end;
end
%% Convolution approach
E = zeros(height, width, channel);
for k=1:channel
Magx = conv2(image(:,:,k), kernelx, 'same');
Magy = conv2(image(:,:,k), kernely, 'same');
E(:,:,k) = sqrt(Magx .^2 + Magy .^2);
end
imshow(sqrt(E(:,:,1).^2 + E(:,:,2).^2 + E(:, :, 3) .^2 ), []);
print('result.png', '-dpng');