ropensci / opencv

R bindings for OpenCV
https://docs.ropensci.org/opencv
Other
137 stars 27 forks source link

cvmat_bgr #22

Closed jwijffels closed 4 years ago

jwijffels commented 4 years ago

Would be good to have such code included in base.cpp to pass common image data directly to opencv. And maybe map that option to ocv_read

// [[Rcpp::export]]
XPtrMat cvmat_bgr(Rcpp::RawVector img, int width = 0, int height = 0){
  std::vector<uchar> x = Rcpp::as<std::vector<uchar>>(img);
  cv::Mat output(height, width, CV_8UC3, x.data());
  return cvmat_xptr(output);
}

Such that one can do

x <- magick::image_read("C:/Users/Jan/Desktop/OCR-HTR/RABrugge_TBO119_693_088.jpg")
width  <- image_info(x)$width
height <- image_info(x)$height
x   <- image_data(x, channels = "bgr")
img <- image.textlinedetector:::cvmat_bgr(x, width = width, height = height)
img
# or img <- opencv::ocv_read(x, type = "bgr")
jwijffels commented 4 years ago

Or similarly for reading only 1 channel (e.g. black/white images)

// [[Rcpp::export]]
XPtrMat cvmat_bw(Rcpp::RawVector img, int width = 0, int height = 0){
  std::vector<uchar> x = Rcpp::as<std::vector<uchar>>(img);
  cv::Mat output(height, width, CV_8U, x.data());
  return cvmat_xptr(output);
}
jwijffels commented 4 years ago

Just listing more functions which I think could be useful

// [[Rcpp::export]]
XPtrMat cvmat_rect(XPtrMat ptr, int x, int y, int width = 0, int height = 0){
  cv::Mat img = get_mat(ptr);
  cv::Rect roi = cv::Rect(x, y, width, height);
  cv::Mat output = img(roi);
  return cvmat_xptr(output);
}
jeroen commented 4 years ago

Would you like to be co-maintainer of this package? It would be helpful if somebody that has domain knowledge can help.

jwijffels commented 4 years ago

Yes, why not. Not sure if I am a complete computer vision expert but I sure need it in some work and happy to contribute.

jeroen commented 4 years ago

OK I'll give you write access so you can make changes.

jwijffels commented 4 years ago

Sounds good. What is your opinion on the opencv package. E.g. could such things (or similar) be integrated https://github.com/DIGI-VUB/image.textlinedetector or will this package remain doing core opencv things?

dselivanov commented 4 years ago

I'm for core things only.

jwijffels commented 4 years ago

Added cvmat_raw_bgr and cvmat_raw_bw in https://github.com/ropensci/opencv/commit/b72e8190abaa9f3f65e02024fedee8026aa26f24 I'll start extending it with some core things.