cv.readImage: add flags arguments plus update example

This commit is contained in:
Simon Hailes 2017-11-04 14:50:01 +00:00
parent 5d05f22d55
commit 2dd3745c81
2 changed files with 56 additions and 9 deletions

View File

@ -1,6 +1,12 @@
var cv = require('../lib/opencv'); var cv = require('../lib/opencv');
// read as a buffer for decode operations later
var fs = require('fs');
var imgdata = fs.readFileSync("./files/mona.png");
var img = cv.readImage("./files/mona.png"); var img = cv.readImage("./files/mona.png");
console.log('Synchronous readImage("./files/mona.png")'+img.width()+'x'+img.height()); console.log('Synchronous readImage("./files/mona.png")'+img.width()+'x'+img.height());
@ -10,18 +16,43 @@ cv.readImage("./files/mona.png", function(err, im){
}); });
img = cv.readImage( 100, 100 ); img = cv.readImage( 100, 100 );
console.log('Synchronous readImage(100, 100) (create mat)'+img.width()+'x'+img.height()); console.log('Synchronous readImage(100, 100) (create mat)'+img.width()+'x'+img.height()+' type '+img.type());
cv.readImage(100, 100, function(err, im){ cv.readImage(100, 100, function(err, im){
console.log('callback readImage(100, 100, fn(){}) (create mat)'+im.width()+'x'+im.height()); console.log('callback readImage(100, 100, fn(){}) (create mat)'+im.width()+'x'+im.height());
}); });
var fs = require('fs');
var imgdata = fs.readFileSync("./files/mona.png");
img = cv.readImage(imgdata); img = cv.readImage(imgdata);
console.log('Synchronous readImage(imgdata:Buffer)'+img.width()+'x'+img.height()); console.log('Synchronous readImage(imgdata:Buffer)'+img.width()+'x'+img.height());
cv.readImage(imgdata, function(err, im){ cv.readImage(imgdata, function(err, im){
console.log('callback readImage(imgdata:Buffer, fn(){})'+im.width()+'x'+im.height()); console.log('callback readImage(imgdata:Buffer, fn(){})'+im.width()+'x'+im.height());
}); });
// try with flags now
console.log('Now with flags');
img = cv.readImage("./files/mona.png", 0);
console.log('Synchronous readImage("./files/mona.png", 0) (monochrome)'+img.width()+'x'+img.height()+' type '+img.type());
cv.readImage("./files/mona.png", 1, function(err, im){
console.log('callback readImage("./files/mona.png", 1, fn(){}) (colour)'+im.width()+'x'+im.height()+' type '+im.type());
});
img = cv.readImage( 100, 100, cv.Constants.CV_8UC3 );
console.log('Synchronous readImage(100, 100, cv.Constants.CV_8UC3) (create 8 bit 3 channel mat)'+img.width()+'x'+img.height()+' type '+img.type());
cv.readImage(100, 100, cv.Constants.CV_8UC1, function(err, im){
console.log('callback readImage(100, 100, cv.Constants.CV_8UC1, fn(){}) (create mat)'+im.width()+'x'+im.height()+' type '+im.type());
});
img = cv.readImage(imgdata, 0);
console.log('Synchronous readImage(imgdata:Buffer, 0) (monochrome)'+img.width()+'x'+img.height()+' type '+img.type());
cv.readImage(imgdata, 1, function(err, im){
console.log('callback readImage(imgdata:Buffer, 1, fn(){}) (colour)'+im.width()+'x'+im.height()+' type '+im.type());
});

View File

@ -42,25 +42,41 @@ NAN_METHOD(OpenCV::ReadImage) {
if (info[0]->IsNumber() && info[1]->IsNumber()) { if (info[0]->IsNumber() && info[1]->IsNumber()) {
int width, height; int width, height;
int type = CV_64FC1;
// if we have a type arg
if ((numargs > 2) && info[2]->IsNumber()){
type = info[2]->Uint32Value();
}
width = info[0]->Uint32Value(); width = info[0]->Uint32Value();
height = info[1]->Uint32Value(); height = info[1]->Uint32Value();
mat = *(new cv::Mat(width, height, CV_64FC1)); mat = *(new cv::Mat(width, height, type));
} else if (info[0]->IsString()) { } else if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString())); std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
mat = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED); int flags = CV_LOAD_IMAGE_UNCHANGED;
if (numargs > 1){
if (info[1]->IsNumber()){
flags = info[1]->Uint32Value();
}
}
mat = cv::imread(filename, flags);
} else if (Buffer::HasInstance(info[0])) { } else if (Buffer::HasInstance(info[0])) {
uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject()); uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject());
unsigned len = Buffer::Length(info[0]->ToObject()); unsigned len = Buffer::Length(info[0]->ToObject());
int flags = CV_LOAD_IMAGE_UNCHANGED;
if (numargs > 1){
if (info[1]->IsNumber()){
flags = info[1]->Uint32Value();
}
}
cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf); cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf);
mat = cv::imdecode(*mbuf, CV_LOAD_IMAGE_UNCHANGED); mat = cv::imdecode(*mbuf, flags);
if (mat.empty()) { if (mat.empty()) {
success = 0; success = 0;
argv[0] = Nan::Error("Error loading file"); argv[0] = Nan::Error("Error loading file");
} }
} }
img->mat = mat; img->mat = mat;