-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConvert.cpp
More file actions
157 lines (128 loc) · 4.28 KB
/
Convert.cpp
File metadata and controls
157 lines (128 loc) · 4.28 KB
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
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
155
156
157
#include <iostream>
#include "Convert.h"
// convert Mat of OpenCV to MomVop image of XM sotfware
void Convert :: ipl2momvop(Mat& src, MomVop* dst)
{
assert( !src.empty() );
assert( dst ); // none should be NULL or abort will be called
assert( dst->y_chan );
//single band
if(src.channels() == 1 && dst->y_chan )
{
for ( int i = 0; i < src.rows; i++)
for( int j = 0; j < src.cols; j++)
dst->y_chan->data->u[i*src.cols + j] = (unsigned char)src.at<uchar>(i,j);
}
// 3-channel color image: in openCV BGR (blue green red); RGB is VYU in MomVop => r:v, g:y, b:u
else if( src.channels() == 3 && dst->y_chan && dst->u_chan && dst->v_chan ){
int index;
for ( int i = 0; i < src.rows; i++)
{
for( int j = 0; j < src.cols; j++)
{
Vec3b& pbgr = src.at<Vec3b>(i,j);
index = i*src.cols + j;
dst->v_chan->data->u[index] = (unsigned char)pbgr[2]; // red
dst->y_chan->data->u[index] = (unsigned char)pbgr[1]; // green
dst->u_chan->data->u[index] = (unsigned char)pbgr[0]; // blue
}
}
}else{
std::cerr<<"ipl2momvop:Input and output images should have 1 or 3 channel(s)"<<std::endl;
exit(0);
}
}
/**
* set alpha channel of MomVop image according to the shape in mask image
* val: value of the region in mask
* fval (destination foreground pixel value): foreground pixels are set to destVal in MomVop img (default 1)
* Background pixels are 0
* note: output image should have been allocated, alpha channel allocated if it does not exist
* note: image sizes should be compatible (not checked)
*/
void Convert :: setShape( Mat& mask, MomVop* img, int val, int fval, Rect* roi )
{
assert( !mask.empty() );
assert( img ); // none should be NULL or abort will be called
if(!img->a_chan){
img->a_chan = initimg (img->width, img->height, UCHAR_TYPE); // sets data to 0
std::cerr << "\nWarning: alpha channel does not exist! Allocated." << std::endl;
}
int x1, x2, y1, y2;
// if no ROI is specified consider the whole image
if(!roi){
x1 = y1 = 0;
x2 = mask.cols;
y2 = mask.rows;
}
else
{
x1 = roi->x;
y1 = roi->y;
x2 = roi->x + roi->width - 1;
y2 = roi->y + roi->height - 1;
}
for ( int i = y1; i < y2; i++){
for( int j = x1; j < x2; j++){
int pval = mask.at<uchar>(i,j);
//if this pixel belongs to the foreground region represented by val
if( pval == val )
img->a_chan->data->u[i*img ->width+j] = (unsigned char)fval;
}
}
}
/**
* set alpha channel of MomVop image according to the shape in mask image
* val: value of the region in mask
* bval: background pixels (default 0)
* fval: foreground pixels are set to destVal in MomVop img (default 1)
* note: output image should have been allocated, alpha channel allocated if it does not exist
* note: image sizes should be compatible (not checked)
*/
void Convert :: setShape( Mat& mask, MomVop* img, int val, int bval, int fval, Rect* roi )
{
assert( !mask.empty() );
assert( img ); // none should be NULL or abort will be called
if(!img->a_chan){
img->a_chan = initimg (img->width, img->height, UCHAR_TYPE);
std::cerr<<"\nWarning: alpha channel does not exist! Allocated."<<std::endl;
}
int x1, x2, y1, y2;
// if no ROI is specified consider the whole image
if(!roi){
x1 = y1 = 0;
x2 = mask.cols;
y2 = mask.rows;
}
else
{
x1 = roi->x;
y1 = roi->y;
x2 = roi->x + roi->width - 1;
y2 = roi->y + roi->height - 1;
}
CvScalar s;
for ( int i = y1; i < y2; i++){
for( int j = x1; j < x2; j++){
int pval = mask.at<uchar>(i,j);
//if this pixel belongs to the foreground region represented by val
if( pval == val )
img->a_chan->data->u[i*img ->width+j] = (unsigned char)fval;
else
img->a_chan->data->u[i*img ->width+j] = (unsigned char)bval;
}
}
}
// Set all the values of the mask (a_chan) to fval
void Convert :: setMaskValue( MomVop* img, int fval )
{
if(!img->a_chan){
std::cerr<<"\nWarning: alpha channel does not exist!"<<std::endl;
return;
}
for ( int i = 0; i < img->height; i++){
for( int j = 0; j < img->width; j++){
img->a_chan->data->u[i*img ->width+j] = (unsigned char)fval;
}
}
}