0% found this document useful (0 votes)
28 views

Image

This document contains the source code for an Image class defined in the LibSubspace namespace. The Image class represents grayscale images and provides methods for accessing pixel values, basic image processing operations like cropping and filtering, and properties like width and height. It stores pixel data in a single unsigned char array and uses integer indexes to access color channel values for a given pixel.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Image

This document contains the source code for an Image class defined in the LibSubspace namespace. The Image class represents grayscale images and provides methods for accessing pixel values, basic image processing operations like cropping and filtering, and properties like width and height. It stores pixel data in a single unsigned char array and uses integer indexes to access color channel values for a given pixel.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3



LPDJHKOLEVXEVSDFH$&OLEUDU\IRUSDWWHUQUHFRJQLWLRQLQVXEVSDFHV*RRJOH3URMHFW+RVWLQJ

VDJDUSM#JPDLOFRP_0\IDYRULWHV_3URILOH_6LJQRXW

OLEVXEVSDFH
$&OLEUDU\IRUSDWWHUQUHFRJQLWLRQLQVXEVSDFHV
3URMHFW+RPH

:LNL

,VVXHV

6RXUFH

 6HDUFKSURMHFWV

([SRUWWR*LW+XE

5($'21/<7KLVSURMHFWKDVEHHQDUFKLYHG)RUPRUHLQIRUPDWLRQVHHWKLVSRVW
&KHFNRXW%URZVH&KDQJHV
6RXUFHSDWK VYQ WUXQNVRXUFHLPDJHK
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

//Copyright (C) 2011 by Ivan Fratric


//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

r3

6KRZGHWDLOV

#pragma once
namespace LibSubspace {
//a simple image class with some (very basic) image processing operations
class Image
{
friend class ImageIO;
protected:
unsigned char *data;
long width;
long height;
long round(float x);
float interpolate(float x1,float x2,float x3, float x4, float dx, float dy);
public:

struct



};

Pixel {
unsigned char B;
unsigned char G;
unsigned char R;

//constructors/destructor
Image(void);
Image(long width, long height);
Image(Image& src);
Image& operator=(const Image &src);
~Image(void);

//initializes the image of specified width and height


//all pixels are set to black
void Init(long width, long height);

//property getters
long GetWidth();
long GetHeight();
unsigned char *GetData();

//pixel getters and setters


unsigned char GetPixelGray(long x,long y);
unsigned char GetPixelRed(long x,long y);
unsigned char GetPixelGreen(long x,long y);
unsigned char GetPixelBlue(long x,long y);
Image::Pixel GetPixelColor(long x,long y);

KWWSVFRGHJRRJOHFRPSOLEVXEVSDFHVRXUFHEURZVHWUXQNVRXUFHLPDJHK"U 





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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

LPDJHKOLEVXEVSDFH$&OLEUDU\IRUSDWWHUQUHFRJQLWLRQLQVXEVSDFHV*RRJOH3URMHFW+RVWLQJ

void
void
void
void
void

SetPixelGray(long x,long y, unsigned char c);


SetPixelColor(long x,long y, Image::Pixel rgb);
SetPixelRed(long x,long y, unsigned char c);
SetPixelGreen(long x,long y, unsigned char c);
SetPixelBlue(long x,long y, unsigned char c);

//returns the color of a pixel at real coordinates (x,y)


//using bilinear interpolation
Image::Pixel GetPixelBilinear(float x, float y);
/////////////////////////////////////////
//some basic image processing functions//
/////////////////////////////////////////

//returns the mean value of pixel intensity


unsigned char GetMeanGray();
//computes the histogram of image intensity and returns it via histogram parameter
//parameters:
// histogram : an array of 256 components, used to return the histogram
void GetHistogramGray(long *histogram);
//binarizes the image
//all pixels with the intensity lower than threshold become black
//all others become white
void Binarize(unsigned char threshold);
//flips the image in horizontal direction
void FlipHorizontal();

//flips the image in vertical direction


void FlipVertical();
//inverts image colors
void Invert();

//crops the image


//returns the subimage with upper left corner at (posx,posy)
//the returned image is of size (width, height)
Image Crop(int posx, int posy, int width, int height);

//resizes the image for a intager facor


//for example, if factor is 2, returns the image with size (width/2, height/2)
//each pixel in a new image is obtained as an average of corresponding pixels in the
original image
Image Resize(int factor);
//computes the convolution of image and a filter
//parameters
// filter : an filterwidth x filterheight array containing the filter
coefficients
// filterwidth : width of the filter
// filterheight : height of the filter
// zero : a value that will be added to each pixel component after filtering,
0 by default
Image Filter(float *filter, long filterwidth, long filterheight);
//filters the image using Gaussian blur
//parameters
// sigma : the standard deviation of the Gaussian filter
// masksize : the size of the corresponding filter
// if set to 0, the masksize will be calculated as
sigma*2*2+1
Image GaussBlur(float sigma, long masksize = 0);
};
inline unsigned char Image::GetPixelGray(long x,long y) {
unsigned char c;
long index = 3*((y*width)+x);
c = (unsigned char)(((long)(data[index])+(long)(data[index+1])+(long)
(data[index+2]))/3);
return c;
}
inline unsigned char Image::GetPixelRed(long x,long y) {
long index = 3*((y*width)+x);
return data[index];
}
inline unsigned char Image::GetPixelGreen(long x,long y) {
long index = 3*((y*width)+x)+1;
return data[index];
}
inline unsigned char Image::GetPixelBlue(long x,long y) {
long index = 3*((y*width)+x)+2;

KWWSVFRGHJRRJOHFRPSOLEVXEVSDFHVRXUFHEURZVHWUXQNVRXUFHLPDJHK"U 





155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

LPDJHKOLEVXEVSDFH$&OLEUDU\IRUSDWWHUQUHFRJQLWLRQLQVXEVSDFHV*RRJOH3URMHFW+RVWLQJ

return data[index];
}
inline void Image::SetPixelRed(long x,long y, unsigned char c) {
long index = 3*((y*width)+x);
data[index]=c;
}
inline void Image::SetPixelGreen(long x,long y, unsigned char c) {
long index = 3*((y*width)+x)+1;
data[index]=c;
}
inline void Image::SetPixelBlue(long x,long y, unsigned char c) {
long index = 3*((y*width)+x)+2;
data[index]=c;
}
inline






}

Image::Pixel Image::GetPixelColor(long x,long y) {


Image::Pixel rgb;
long index = 3*((y*width)+x);
rgb.B = data[index];
rgb.G = data[index+1];
rgb.R = data[index+2];
return rgb;

inline




}

void Image::SetPixelGray(long x,long y, unsigned char c) {


long index = 3*((y*width)+x);
data[index] = c;
data[index+1] = c;
data[index+2] = c;

inline








}

void Image::SetPixelColor(long x,long y, Image::Pixel rgb) {


if(x<0) return;
if(y<0) return;
if(x>=width) return;
if(y>=height) return;
long index = 3*((y*width)+x);
data[index] = rgb.B;
data[index+1] = rgb.G;
data[index+2] = rgb.R;

inline long Image::GetWidth() {


return width;
}
inline long Image::GetHeight() {
return height;
}
inline unsigned char *Image::GetData() {
return data;
}
} //namespace

7HUPV3ULYDF\3URMHFW+RVWLQJ+HOS
3RZHUHGE\*RRJOH3URMHFW+RVWLQJ

KWWSVFRGHJRRJOHFRPSOLEVXEVSDFHVRXUFHEURZVHWUXQNVRXUFHLPDJHK"U 



You might also like