Wednesday, September 17, 2014

Simpson's One-Third Rule of numerical integration in C

#include<stdio.h>
#include<conio.h>

float f(float x){
float r;
r=2*x+1;
return r;
}

void main(){

float output;
float a,b;
float h,s;
int i,j;
int n;
clrscr();
printf("===========================================\n");
printf("           SIMPSON'S ONE-THIRD RULE\n");
printf("===========================================\n");
printf("Enter the integral limit:-\n");
printf("-------------------------------------------\n");
printf("Lower limit value a: \n");
scanf("%f",&a);
printf("Upper limit value b: \n");
scanf("%f",&b);
printf("-------------------------------------------\n");
printf("Enter number of total intervals: \n");
scanf("%d",&n);
h=0.0;
h=(b-a)/(2*n);
printf("____________________________________________\n");
printf("Resultant value of h is %.2f \n",h);
s=0.0;
s=f(a)+f(b)+4*f(a+h);
printf("Resultant value of f(a) + f(b) is %.2f \n",s);
for (i=3;i<2*n;i+=2)
{
s+=2*f(a+(i-1)*h)+4*f(a+i*h);
}
printf("Resultant value of whole summation of f(-) is %.2f\n",s);
output=1;
output=(h/3)*s;
printf("Output value of the integral is %.2f \n",output);
printf("____________________________________________\n");
printf("             GOOD LUCK\n");
getch();
}

Tuesday, December 3, 2013

Write a program in MatLab to extract a part of image using without a function .


clc;
clear all;
close all;

A=imread('contrast (3).tif');
r=size(A,1)-1;
i=int32(rand(1,1)*r)+1; % +1 for random value always from 0
j=i+500;
if(j>size(A,1))
    d=j-size(A,1);
    j=j-d;
end
c=size(A,2)-1;
k=int32(rand(1,1)*c)+1; % +1 for random value always from 0
l=k+500;
if(l>size(A,2))
    d1=l-size(A,2);
    l=l-d1;
end
B=zeros(size(A,1),size(A,2));
x=i;
for p=i:j-1
    y=k;
    for q=k:l-1
        B(x,y)=A(p,q);
        y=y+1;
    end
    x=x+1;
end
B1 = mat2gray(B);
figure('Position',[10  50 1400 630]);
subplot(1,2,1),imshow(A),
ylabel('Original Image','Color','r','FontSize', 10,'FontAngle', 'italic');
subplot(1,2,2),imshow(B1),
ylabel('Sub Image','Color','r','FontSize', 10,'FontAngle', 'italic');

Write a program in MatLab to enhance brightness and contrast of RGB and Gray images.

clc;
clear all;
close all;

im=imread('contrast (3).tif');
d=ndims(im);
if(d==2)
%----------------------------------------------------------
%                       Gray Image
%----------------------------------------------------------
    im=double(im);
    [M N]=size(im);
    MN=M*N;
    sum=0;
    for i=1:M
        for j=1:N
            sum = sum + im(i,j);
        end
    end
    B=sum/MN;
    disp(['Brightness of an Gray Image is ',num2str(B)]);
    sum=0;
    for i=1:M
        for j=1:N
            S=im(i,j)-B;
            S=S^2;
            sum = sum + S;
        end
    end
    C=sum/MN;
    con=double(C)^0.5;
    disp(['Contrast of an Gray Image is ',num2str(con)]);
else
%----------------------------------------------------------
%                       RGB
%----------------------------------------------------------
    B_sum=0;
    C_sum=0;
    for k=1:3
        I=im(:,:,k);
        I=double(I);
        [M N]=size(I);
        MN=M*N;
        sum=0;
        for i=1:M
            for j=1:N
                sum = sum + I(i,j);
            end
        end
        B=sum/MN;
        B_sum=B_sum+B;
        sum=0;
        for i=1:M
            for j=1:N
                S=I(i,j)-B;
                S=S^2;
                sum = sum + S;
            end
        end
        C=sum/MN;
        con=double(C)^0.5;
        C_sum=C_sum+con;
    end
    disp(['Brightness of an RGB image is ',num2str(B_sum)]);
    disp(['Contrast of an RGB image is ',num2str(C_sum)]);
end