最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change pixel value in OpenCV.js - Stack Overflow

programmeradmin0浏览0评论

According to OpenCV.js docs to modify a pixel value you can use 3 methods:

  1. Direct data manipulation
  2. at() family of methods
  3. ptr() family of methods

Althougth the header of the section in the docs say "Accessing and Modifying pixel values" it provides examples for retrieving a value but not for modifying them. The problem is that while in C++ this code using the at or ptr methods works:

mat.at<type>(row, col) = value;

The equivalent in javascript is not valid and gives an Invalid left hand side in assignment expression:

mat.floatAt(row, col)) = value;

I could make it work using the direct data manipulation method with:

mat.data[row * this.cols * this.channels() + col * this.channels()] = value;

But this method does not operate in pixel values but in the underlaying array data structure where a pixel may span more than one array index, so is not valid for my use case.

How can a pixel value at [row, col] position in a CvMat be modified using OpenCV.js?

According to OpenCV.js docs to modify a pixel value you can use 3 methods:

  1. Direct data manipulation
  2. at() family of methods
  3. ptr() family of methods

Althougth the header of the section in the docs say "Accessing and Modifying pixel values" it provides examples for retrieving a value but not for modifying them. The problem is that while in C++ this code using the at or ptr methods works:

mat.at<type>(row, col) = value;

The equivalent in javascript is not valid and gives an Invalid left hand side in assignment expression:

mat.floatAt(row, col)) = value;

I could make it work using the direct data manipulation method with:

mat.data[row * this.cols * this.channels() + col * this.channels()] = value;

But this method does not operate in pixel values but in the underlaying array data structure where a pixel may span more than one array index, so is not valid for my use case.

How can a pixel value at [row, col] position in a CvMat be modified using OpenCV.js?

Share Improve this question asked May 2, 2019 at 10:09 David CasillasDavid Casillas 1,9211 gold badge30 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I have been successful in setting single pixels of images (or matrices) with ucharPtr.

Instead of setting it like this:

img.ucharPtr(i, j) = 255

I set it like this:

img.ucharPtr(i, j)[0] = 255 

Even if the image is black and white and only has one channel

If you want to set all 4 pixel values, you can do this:

src.ucharPtr(i, j)[0] = 255
src.ucharPtr(i, j)[1] = 255 
src.ucharPtr(i, j)[2] = 255 
src.ucharPtr(i, j)[3] = 0 
发布评论

评论列表(0)

  1. 暂无评论