Plotting individual pixel in shoes:Ruby - ruby

i am working with Shoes in Ruby. I could not find a method to plot individual pixels in the shoes window.... Can anyone help me??
:)

I don't think it's possible. See Shoes GUI toolkit per pixel manipulation possible?
I think the closest you can get is a 2x2 square:
Shoes.app do
click{|b, x, y|
rect(x,y,1,1) if b == 1
}
end
Or a 2x1 line:
Shoes.app do
click{|b, x, y|
line(x,y,x+1,y) if b == 1
}
end

That means that you can do something like that if you want to draw a pixel at x,y location, with a particular color assuming you know the background color:
def point(x,y,color, bg_color)
stroke color
line x,y,x,y+1
stroke bg_color
line x,y+1,x+1,y+1
end
Shoes.app do
background white
point 40,40,blue,white
end
:)
Of course for drawing pixels densely it is useless but in some application can be useful.

Related

How to flip image in matlab without using built in functions?

Write a MATLAB code that reads a gray scale image and generates the flipped image of original image.enter image description here
i am trying this code but is not giving me correct flipped image.Help will be much appreciated.Thankyou
clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
for i=r:-1:1
k=1;
for j=1:1:c
temp=a(k,j);
result(k,j)=a(i,j);
result(i,j)=temp;
k=k+1;
end
end
subplot(1,2,1), imshow(a)
subplot(1,2,2),imshow(result)
What you're doing with indices is kind of unclear. You should also pre-allocate memory for the result.
clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
result = a; % preallocate memory for result
for i=1:r
for j=1:c
result(r-i+1,j)=a(i,j);
end
end
subplot(1,2,1), imshow(a)
subplot(1,2,2),imshow(result)
You can use basic indexing to flip a matrix. 2D case (gray-scale image):
a = a(:,end:-1:1); % horizontal flip
a = a(end:-1:1,:); % vertical flip
a = a(end:-1:1,end:-1:1); % flip both: 180 degree rotation
For the 3D case (color image) add a 3rd index ::
a = a(:,end:-1:1,:); % horizontal flip

How to get the four corners of a rotated image?

I have an image rotated with imrotate as follow:
Im_requete=imread('lena.jpg');
Im_requete_G=rgb2gray(Im_requete);
Im_requete_G_scale_rot = imresize(imrotate(Im_requete_G,-20), 1.2);
I'm trying to get the coordinates (x, y) of the four corners of the rotated image as illustrated in the image below (red circle represents the desired corner):
This is my code:
stat = regionprops(Im_requete_G_scale_rot,'Extrema'); %extrema detection of the image.
point = stat.Extrema;
hold on
figure,imshow(Im_requete_G_scale_rot)
hold on
for i = 2:2:length(point)
x = point(i,1);
y = point(i,2);
plot(x,y,'o');
text(x,y,num2str(i),'color','r')
end
But the resulting coordinates are somewhere along the edges and not where I wanted them to be, as illustrated in the second image:
Can someone please tell me what's wrong with this code?
I don't have a good explanation for this, but I suppose regionprops gets confused by the grayscale tones in the image. If we turn the rotated Lena into a logical array, your algorithm works properly:
Im_requete_G_scale_rot = logical(imresize(imrotate(Im_requete_G,-20), 1.2)); % 3rd line

Matlab detect rectangle from image [duplicate]

I need to know how to align an image in Matlab for further work.
for example I have the next license plate image and I want to recognize all
the digits.
my program works for straight images so, I need to align the image and then
preform the optical recognition system.
The method should be as much as universal that fits for all kinds of plates and in all kinds of angles.
EDIT: I tried to do this with Hough Transform but I didn't Succeed. anybody can help me do to this?
any help will be greatly appreciated.
The solution was first hinted at by #AruniRC in the comments, then implemented by #belisarius in Mathematica. The following is my interpretation in MATLAB.
The idea is basically the same: detect edges using Canny method, find prominent lines using Hough Transform, compute line angles, finally perform a Shearing Transform to align the image.
%# read and crop image
I = imread('http://i.stack.imgur.com/CJHaA.png');
I = I(:,1:end-3,:); %# remove small white band on the side
%# egde detection
BW = edge(rgb2gray(I), 'canny');
%# hough transform
[H T R] = hough(BW);
P = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
%# shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
Now lets see the results
%# show edges
figure, imshow(BW)
%# show accumlation matrix and peaks
figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho'), colormap(hot), colorbar
hold on, plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2), hold off
axis on, axis normal
%# show image with lines overlayed, and the aligned/rotated image
figure
subplot(121), imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end, hold off
subplot(122), imshow(II)
In Mathematica, using Edge Detection and Hough Transform:
If you are using some kind of machine learning toolbox for text recognition, try to learn from ALL plates - not only aligned ones. Recognition results should be equally well if you transform the plate or dont, since by transforming, no new informations according to the true number will enhance the image.
If all the images have a dark background like that one, you could binarize the image, fit lines to the top or bottom of the bright area and calculate an affine projection matrix from the line gradient.

How can I rotate a rectangle and position using MiniMagick?

I'm trying to create an overlay using mini_magick (Ruby wrapper for imagemagick) similar to the example below:
I know how to draw a rectangle, and I know how to rotate, but I can't wrap my head around the coordinate system once the rotation has been applied.
So I wrote the following:
x = original_icon.width / 2
y = 0 # or should it be -10 so it's off the canvas?
width = 100
height = 20
result = original_icon.combine_options do |c|
c.fill('#FF0000')
c.draw "rotate -45 rectangle #{x},#{y},#{width},#{height}"
end
However, things get odd fast after the rotate -45. It seems positioning has completely gone out the window.
Is there another way to achieve this?

Create a diamond in the picture

I'm going to write a program that does a diamond within a specific image
i wrote this code
Img=zeros(256,256);
for x=1:256
for y=1:256
if(y-x==128)||(y-x==-128)||(y+x==128)
Img(x,y)=1;
end
end
end
imshow(Img);
I could draw three sides diamond.
How do I draw a fourth side؟
Luis Mendo nailed the last condition in the if statement of your loop to draw the last line. His solution is what you are looking for should you want a for loop solution.
However, if you want something more vectorized, a more efficient way to do this would be to define a grid of co-ordinates using meshgrid then simply apply a chain of logical operations to get your diamond. In other words:
[x,y] = meshgrid(1:256, 1:256);
Img = abs(x-y) == 128 | x+y == 384 | x+y == 128;
imshow(Img);
We thus get:
In general, for a given size image that is square, you can adapt this to any size image by:
N = 256; %// Change here - width/height of your image
[x,y] = meshgrid(1:N, 1:N);
Img = abs(x-y) == floor(N/2) | x+y == floor(3*N/2) | x+y == floor(N/2);
imshow(Img);
I've placed a floor operation in case you want to specify an image size that is not evenly divisible by 2. However, the symmetry of the diamond may not look correct, but the code will work regardless.
The condition you are missing is y+x==384. So, replace the if line by
if (y-x==128)||(y-x==-128)||(y+x==128)||(y+x==384)
The condition would look more symmetric if your coordinates were centered around 0, say -128:128:
Img=zeros(257,257);
for x=-128:128
for y=-128:128
if(y-x==128)||(y-x==-128)||(y+x==128)||(y+x==-128)
Img(129+x,129+y)=1;
end
end
end
imshow(Img);
As noted by #kkuilla, this can be easily vectorized. See #rayryeng's answer.

Resources