Convolving two images - convolution

This is my coding for convolving my image and patch of 16*16 of same image.I changed my coding now.But I am getting only image full of white pixels? This is my changed coding
clear all
close all
I=imread('E:\double.jpg');
subplot(3,2,1);
imshow(I);
title('Original');
%gray conversion
J = rgb2gray(I);
subplot(3,2,2);
imshow(J);
title('Gray');
K = double(J);
%size of the image
[A B]=size(J);
%patch definition
patch_size = 16;
temp = J(1:patch_size, 1:patch_size);
subplot(3,2,3);
imshow(temp);
title('kernel');
temp1 = double(temp);
C = conv2(temp1,K);
subplot(3,2,4);
imshow(C);
title('Convolved image');

Related

Detecting lanes in a video using matlab and image processing.

I am a little bit new to matlab and imageprocessing and I was given a task at my faculty to carry out a project which detects the lanes for a moving car in a video. I tried to use some tutorials on Mathworks and other sites and there were really helpful and I came out with a code that detects lanes in an image and I just want to know how to apply my code on a video as I see it working properly on an image.
and here is my code :
img = imread ('test_image.jpg');
I = rgb2gray (img);
%making a gaussian kernel
sigma = 1 ; %standard deviation of distribution
kernel = zeros (5,5); %for a 5x5 kernel
W = 0 ;
for i = 1:5
for j = 1:5
sq_dist = (i-3)^2 + (j-3)^2 ;
kernel (i,j) = exp (-1*exp(sq_dist)/(2*sigma));
W = W + kernel (i,j) ;
end
end
kernenl = kernel/W ;
%Now we apply the filter to the image
[m,n] = size (I) ;
output = zeros (m,n);
Im = padarray (I , [2 2]);
for i=1:m
for j=1:n
temp = Im (i:i+4 , j:j+4);
temp = double(temp);
conv = temp.*kernel;
output(i,j) = sum(conv(:));
end
end
output = uint8(output);
%--------------Binary image-------------
level = graythresh(output);
c= im2bw (output,level);
%---------------------------------------
output2 = edge (c , 'canny',level);
figure (1);
%Segment out the region of interest
ROI = maskedImage;
CannyROI = edge (ROI , 'canny',.45);
%----------------------------------
set (gcf, 'Position', get (0,'Screensize'));
%subplot (141), imshow (I), title ('original image');
%subplot (142), imshow (c), title ('Binary image');
%subplot (143), imshow (output2), title ('Canny image');
%subplot (144), imshow (CannyROI), title ('ROI image');
[H ,T ,R] = hough(CannyROI);
imshow (H,[],'XData',T,'YData',R,'initialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on , axis normal, hold on ;
P = houghpeaks(H,5,'threshold',ceil (0.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot (x,y,'s','color','white');
%Find lines and plot them
lines = houghlines (CannyROI,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow (img), hold on
max_len = 0 ;
for k = 1:length(lines);
xy = [lines(k).point1; lines(k).point2];
plot (xy(:,1), xy(:,2), 'LineWidth', 5 , 'Color', 'blue');
%plot beginnings and ends of the lines
plot (xy(1,1), xy(1,2),'x', 'LineWidth', 2, 'Color', 'yellow');
plot (xy(2,1), xy(2,2),'x', 'LineWidth', 2, 'Color', 'red');
%determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if (len>max_len)
max_len = len;
xy_long = xy;
end
end
and here is the link of the image and the video :
https://github.com/rslim087a/road-video
https://github.com/rslim087a/road-image
Thanks in advance.
Basically video processing happens in such a way that video will be converted to video frames (images). So if you need, you can convert your video to video frames and run the code, looping over the folder having the video frames. Change the imread function to get images from video frames folder...
img = imread(path_to_video_frames_folder/*)

How to answer this Matlab image processing homework?

I'm stuck here, I tried many times but unable to get my final answer.
Code:
I = imread('C:\Users\Ahsan\Desktop\pears.png');
H = fspecial('average', [3 3]);
J = imfilter(I, H);
figure, imshow(I);
figure, imshow(J);
Try this one:
(Maybe you have to change the threshold);
threshold = 126;
image = imread('C:\Users\Ahsan\Desktop\pears.png');
% Apply the filder
filterImage = conv2(image, ones(3)/9, 'same');
% Check which pixels are equal or greater than the threshold
masked = filterImage >= threshold;
% Replace all pixels of the filteredImage which are below the threshold
% with the original pixels.
filterImage(~masked) = image(~masked);
% Display result
figure(1);
subplot(1,2,1);
imshow(image, []);
subplot(1,2,2);
imshow(filterImage, []);

Resize axes to fit image

How can I resize my axes on a matlab GUI to fit exactly with the size of the image I just resized?
I have this:
I = imread('honolulu.png');
I = imresize(im2double(I), [600, 700]);
After that I am drawing a grid on the pictures, but because the axes size is different the grid does not look good. However, if I create a figure and I do it on a figure outside the GUI it looks perfect.
Full code:
%Load map
I = imread('honolulu.png');
%Resize image to be multiple of 50 in each axis.
I = imresize(im2double(I), [600, 700]);
%Draw grid of 50x50 pixels.
I(50:50:end, :, :) = 255;
I(:, 50:50:end, :) = 255;
axes(handles.axes1);
h = handles.axes1;imshow(I);
while (ishandle(h))
try
[x, y] = ginput(1);
catch me
%break loop in case image is closed.
break;
end
%Compute top left coordinate of block clicked.
x0 = floor((x-1)/50)*50;
y0 = floor((y-1)/50)*50;
%Set block RGB to random color.
I(y0+1:y0+50-1, x0+1:x0+50-1, 1) = rand();
I(y0+1:y0+50-1, x0+1:x0+50-1, 2) = rand();
I(y0+1:y0+50-1, x0+1:x0+50-1, 3) = rand();
imshow(I);
end
% Choose default command line output for city_planning
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
How it looks vs how it is supposed to look
The lines are disappearing as the plot window changes size - something of an aliasing/rendering issue I guess. Elsewhere other people have asked similar questions without a great answer.
The best I've found is setting the initial magnification to 100% and then preventing resizing. Here's a similar example:
close all;
clear all;
clc;
I = imread('peppers.png');
I = imresize(im2double(I), [600, 700]);
%Draw grid of 50x50 pixels.
I(50:50:end, :, :) = 1;
I(:, 50:50:end, :) = 1;
h = imshow(I,'initialMagnification',100);
set(gcf,'Resize','off')
%%
while (ishandle(h))
try
[x, y] = ginput(1);
catch me
%break loop in case image is closed.
break;
end
%Compute top left coordinate of block clicked.
x0 = floor((x-1)/50)*50;
y0 = floor((y-1)/50)*50;
%Set block RGB to random color.
I(y0+1:y0+50-1, x0+1:x0+50-1, 1) = rand();
I(y0+1:y0+50-1, x0+1:x0+50-1, 2) = rand();
I(y0+1:y0+50-1, x0+1:x0+50-1, 3) = rand();
h = imshow(I);
end

How to binarize image, Matlab

I have to binarize image so I have twice as many white pixels as black pixels.
Someone gave me answer:
binarized = im2bw(image, 0.28)
and I'm not sure how do this person know that level 0.28 gives twice as many white as black?
In this code, I had to use gamma correction, use imhist, binarize. My code:
close all;
clear all;
clc;
img = imread('cameraman.tif');
img = double(img)/255;
coeff = 0.6;
gamma = img.^coeff;
figure;
subplot(121); imshow(img); title('Oryginalny');
subplot(122); imshow(gamma); title('Po korekcji gamma');
equalized = histeq(gamma,32);
figure;
subplot(221); imshow(gamma); title('Po korekcji gamma');
subplot(222); imshow(equalized); title('Wyrównany');
subplot(223); imhist(gamma); title('Po korekcji gamma');
subplot(224); imhist(equalized); title('Wyrównany');
SE = strel('disk', 3);
eroded = imerode(equalized,SE);
opening = imdilate(eroded,SE);
figure;
subplot(121); imshow(equalized); title('Wyrównany');
subplot(122); imshow(opening); title('Otwarcie');
binarized = im2bw(opening, 0.28);
figure;
imshow(binarized); title('Po binaryzacji');
w = binarized == 1;
b = binarized == 0;
biale = sum(w(:));
czarne = sum(b(:));

MATLAB Histogram equalization on GIF images

I'm having a bit of trouble understanding how to change a colormap of a grayscale GIF image after performing histogram equalization on the image. The process is perfectly simple with image compression types that don't have an associated colormap, such as JPEG, and I've gotten it to work with grayscale JPEG images.
clear
clc
[I,map] = imread('moon.gif');
h = zeros(256,1); %array value holds number of pixels with same value
hmap = zeros(256,1);
P = zeros(256,1); %probability that pixel intensity will appear in image
Pmap = zeros(256,1);
s = zeros(256,1); %calculated CDF using P
smap = zeros(256,1);
M = size(I,1);
N = size(I,2);
I = double(I);
Inew = double(zeros(M,N));
mapnew = zeros(256,3);
for x = 1:M;
for y = 1:N;
for l = 1:256;
%count pixel intensities and probability
end
end
end
for j = 2:256
for i = 2:j
%calculate CDF of P
end
end
s(1) = P(1);
smap(1) = Pmap(1);
for x = 1:M;
for y = 1:N;
for l = 1:256;
%calculates adjusted CDF and sets it to new image
end
end
end
mapnew = mapnew/256;
Inew = uint8(Inew);
I = uint8(I);
subplot(1,2,1), imshow(Inew,map); %comparing the difference between original map
subplot(1,2,2), imshow(Inew,mapnew); %to'enhanced' colormap, but both turn out poorly
All is fine in terms of the equalization of the actual image, but I'm not sure what to change about the color map. I tried performing the same operations on the colormap that I did with the image, but no dice.
Sorry that I can't post images cause of my low rep, but I'll try and provide all the info I can on request.
Any help would be greatly appreciated.
function J=histeqo(I)
J=I;
[m,n]=size(I);
[h,d]=imhist(I);
ch=cumsum(h); // The cumulative frequency
imagesize=(m*n); // The image size lightsize=size(d,1);// The Lighting range
tr=ch*(lightsize/imagesize); // Adjustment function
for x=1:m
for y=1:n
J(x,y)=tr(I(x,y)+1);
end
end
subplot(1,2,1);imshow(J);
subplot(1,2,2);imhist(J);
end