Skip to content

Commit c4a1c71

Browse files
committed
Fix row and col tests for 1 channel matrices
1 parent 6168713 commit c4a1c71

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/Matrix.cc

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Matrix::Init(Handle<Object> target) {
2424
NODE_SET_PROTOTYPE_METHOD(constructor, "row", Row);
2525
NODE_SET_PROTOTYPE_METHOD(constructor, "col", Col);
2626

27+
NODE_SET_PROTOTYPE_METHOD(constructor, "pixelRow", PixelRow);
28+
NODE_SET_PROTOTYPE_METHOD(constructor, "pixelCol", PixelCol);
2729

2830
NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty);
2931
NODE_SET_PROTOTYPE_METHOD(constructor, "get", Get);
@@ -66,7 +68,7 @@ Matrix::Matrix(): ObjectWrap() {
6668
}
6769

6870
Matrix::Matrix(int w, int h): ObjectWrap() {
69-
mat = cv::Mat(w, h, CV_32FC1);
71+
mat = cv::Mat(w, h, CV_32FC3);
7072
}
7173

7274
Handle<Value>
@@ -116,6 +118,33 @@ Handle<Value>
116118
Matrix::Row(const Arguments& args){
117119
SETUP_FUNCTION(Matrix)
118120

121+
int width = self->mat.size().width;
122+
int y = args[0]->IntegerValue();
123+
v8::Local<v8::Array> arr = v8::Array::New(width);
124+
125+
for (int x=0; x<width; x++){
126+
double v = 0;
127+
if (self->mat.channels() == 1){
128+
v = self->mat.at<float>(y, x);
129+
} else {
130+
// Assume 3 channel RGB
131+
unsigned int val = 0;
132+
cv::Vec3b pixel = self->mat.at<cv::Vec3b>(y, x);
133+
val &= (uchar) pixel.val[2];
134+
val &= ((uchar) pixel.val[1]) << 8;
135+
val &= ((uchar) pixel.val[0]) << 16;
136+
v = (double) val;
137+
}
138+
arr->Set(x, Number::New(v));
139+
}
140+
return scope.Close(arr);
141+
}
142+
143+
144+
Handle<Value>
145+
Matrix::PixelRow(const Arguments& args){
146+
SETUP_FUNCTION(Matrix)
147+
119148
int width = self->mat.size().width;
120149
int y = args[0]->IntegerValue();
121150
v8::Local<v8::Array> arr = v8::Array::New(width * 3);
@@ -134,6 +163,33 @@ Handle<Value>
134163
Matrix::Col(const Arguments& args){
135164
SETUP_FUNCTION(Matrix)
136165

166+
int height = self->mat.size().height;
167+
int x = args[0]->IntegerValue();
168+
v8::Local<v8::Array> arr = v8::Array::New(height);
169+
170+
for (int y=0; y<height; y++){
171+
double v = 0;
172+
if (self->mat.channels() == 1){
173+
v = self->mat.at<float>(y, x);
174+
} else {
175+
// Assume 3 channel RGB
176+
unsigned int val = 0;
177+
cv::Vec3b pixel = self->mat.at<cv::Vec3b>(y, x);
178+
val &= (uchar) pixel.val[2];
179+
val &= ((uchar) pixel.val[1]) << 8;
180+
val &= ((uchar) pixel.val[0]) << 16;
181+
v = (double) val;
182+
}
183+
arr->Set(y, Number::New(v));
184+
}
185+
return scope.Close(arr);
186+
}
187+
188+
189+
Handle<Value>
190+
Matrix::PixelCol(const Arguments& args){
191+
SETUP_FUNCTION(Matrix)
192+
137193
int height = self->mat.size().height;
138194
int x = args[0]->IntegerValue();
139195
v8::Local<v8::Array> arr = v8::Array::New(height * 3);
@@ -225,7 +281,7 @@ Matrix::Eye(const v8::Arguments& args){
225281

226282
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
227283
Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
228-
cv::Mat mat = cv::Mat::eye(w, h, CV_32FC1);
284+
cv::Mat mat = cv::Mat::eye(w, h, CV_32F);
229285

230286
img->mat = mat;
231287
return scope.Close(im_h);
@@ -241,7 +297,7 @@ Matrix::Resize(const v8::Arguments& args){
241297
int y = args[1]->Uint32Value();
242298

243299
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
244-
cv::Mat res = cv::Mat(x, y, CV_32FC1);
300+
cv::Mat res = cv::Mat(x, y, CV_32FC3);
245301
cv::resize(self->mat, res, cv::Size(x, y), 0, 0, cv::INTER_LINEAR);
246302
~self->mat;
247303
self->mat = res;

src/Matrix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class Matrix: public node::ObjectWrap {
1313

1414

1515
JSFUNC(Row)
16+
JSFUNC(PixelRow)
1617
JSFUNC(Col)
18+
JSFUNC(PixelCol)
1719
JSFUNC(Eye) // factory
1820

1921
/*

test/smoke.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ vows.describe('Smoke Tests OpenCV').addBatch({
202202
, "finds face": function(err, faces){
203203
assert.isNull(err);
204204
assert.isArray(faces);
205+
console.log(faces);
205206
assert.equal(faces.length, 1)
206207

207208
}

0 commit comments

Comments
 (0)