summaryrefslogtreecommitdiff
path: root/macros/cvMedian.sci
blob: a403984e5a8cbb604b68b7cb422c438debf9b51f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//**************************************************
// Author : Asmita Bhar
//**************************************************

function [out] = cvMedian(image,varargin)
// Finds median values in an input
//
// Calling Sequence
// val = cvMedian(image)
// val = cvMedian(image, name, value, .....)
//
// Parameters
// image : Input image matrix 
// SortMethod (Optional) : The sort method to calculate the median - Quick sort or Insertion Sort. Default : Quick sort
// Dimension (Optional) : Dimension along which the function operates - Row, Column, All or Custom. Default : All
// CustomDimension (Optional) : The integer dimension over which the function calculates the median. This value cannot exceed the number of dimensions in input. It applies only when 'Dimension' property is set to 'Custom'. Default : 1
//
// Description 
// The function calculates the median values in a given input image matrix. 
// 
// Examples
// //Load an image
// I = imread('peppers.png');
// val1 = cvMedian(I); // calculates the median value considering dimension as 'All'
// val2 = cvMedian(I,'Dimension','Row'); //calculates the median value in 'Row' dimension
// 
// Authors
// Asmita Bhar
//

	[lhs,rhs] = argn(0);
	if rhs<1 then
		error(msprintf("Not enough input arguments"));
	end
	if rhs>7 then
		error(msprintf("Too many input arguments"));
	end
	
	[iRows iCols]=size(image(1))
	iChannels = size(image)
	
	sortMethod = 'Quick sort';
	dimension = 'All';
	customDimension = 1;

	flag=0;
	i=1;
	while(i<rhs-1)
		if strcmpi(varargin(i),'SortMethod')==0 then
			sortMethod = varargin(i+1);
			if strcmpi(sortMethod,"Quick sort") & strcmpi(sortMethod,"Insertion sort") then
                		error(msprintf(" wrong input argument #%d,SortMethod not matched",i))
            		end
		elseif strcmpi(varargin(i),'Dimension')==0 then
			dimension = varargin(i+1)
			if strcmpi(dimension,"Column") & strcmpi(dimension,"Row") &strcmpi(dimension,"All") & strcmpi(dimension,"Custom") then
                		error(msprintf(" wrong input argument #%d, Dimension not matched",i))
            		end
		elseif strcmpi(varargin(i),'CustomDimension')==0 then
			customDimension = varargin(i+1)
			flag=1;
		end
		i=i+2;
	end
	
	if (strcmpi(dimension,'Custom') & (flag==1))
		error(msprintf("The CustomDimension property is not relevant in this configuration"));
	end

	if(iChannels==1) then
		if(dimension=='All') then
			out = median(image(1));
		elseif(dimension=='Row') then
			out = median(image(1),'c');
		elseif(dimension=='Column') then
			out = median(image(1),'r');
		elseif(dimension=='Custom') then
			if(customDimension==1) then
				out = median(image(1),'r');
			elseif(customDimension==2) then
				out = median(image(1),'c');
			end
		end
	elseif(iChannels==3)
		if(dimension=='All') then
			out = median([image(1) image(2) image(3)]); 
		elseif(dimension=='Row') then
			out(:,:,1) = median(image(1),'c');
			out(:,:,2) = median(image(2),'c');
			out(:,:,3) = median(image(3),'c');
		elseif(dimension=='Column') then
			out(:,:,1) = median(image(1),'r');
			out(:,:,2) = median(image(2),'r');
			out(:,:,3) = median(image(3),'r');
		elseif(dimension=='Custom') then
			if(customDimension==1) then
				out(:,:,1) = median(image(1),'r');
				out(:,:,2) = median(image(2),'r');
				out(:,:,3) = median(image(3),'r');
			elseif(customDimension==2) then
				out(:,:,1) = median(image(1),'c');
				out(:,:,2) = median(image(2),'c');
				out(:,:,3) = median(image(3),'c');
			elseif(customDimension==3) then
				a = median(image(1),3);
				b = median(image(2),3);
				c = median(image(3),3);
				for i=1:iRows
					for j=1:iCols
						out(i,j) = median([a(i,j) b(i,j) c(i,j)]);	
					end

				end
			end
		end
	end

endfunction