color a grayScale MAT

31 views
Skip to first unread message

shira Jacobs

unread,
Nov 21, 2022, 6:21:09 AM11/21/22
to javacv
Hi Friends, 
I convert a Mat to GrayScale and now I want to color a specific pixel 
is that can be??

I tried to create a new MAT and put the pixel from the grayscale mat one by one and in the specific pixels to put color, 
but I cannot create an Indexer for the GrayScale Mat



thanks for any Help!!!
Shira :)

Samuel Audet

unread,
Nov 21, 2022, 8:03:05 AM11/21/22
to javacv
That's something we can easily do using indexers, yes. Why do you say that isn't working?

--

---
You received this message because you are subscribed to the Google Groups "javacv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacv+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/javacv/2244ba50-25e2-4c50-af6b-7ddf08f2a2a0n%40googlegroups.com.

shira Jacobs

unread,
Nov 21, 2022, 3:19:11 PM11/21/22
to javacv
Thank you Samuel for Your answer
  I'm getting this error while creating an Indexer to the  GrayScale mat


OpenCV(4.5.5) D:\a\javacpp-presets\javacpp-presets\opencv\cppbuild\windows-x86_64\opencv-4.5.5\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'




my code is this please let me know what I'm wrong and what is the best way to do it 


Mat mask = new Mat(diffImage.rows(),diffImage.cols(),CV_8UC1, maskscalar);
Mat maskDiff = new Mat(diffImage.rows(),diffImage.cols(),img0.type(), img0.data());

Mat m3=img0;
opencv_imgproc.cvtColor(m3, m3,opencv_imgproc.COLOR_RGB2GRAY);

final UByteIndexer resizeIdx = diffImage.createIndexer();
final UByteIndexer maskIdx = mask.createIndexer();
final UByteIndexer maskDiffIdx = maskDiff.createIndexer();
final UByteIndexer maskDiffIdx2 = maskDiff2.createIndexer();
final UByteRawIndexer maskDiffIdx3 = m3.createIndexer();

for (long hh = 0; hh < diffImage.rows(); hh++) {
for (long ww = 0; ww < diffImage.cols(); ww++) {
// int a=maskDiffIdx3.get(hh,ww);
if (resizeIdx.get(hh, ww) > 30) {
maskIdx.put(hh, ww,255);
//maskDiffIdx2.put(hh, ww,0);
for (int i = 0; i <maskDiff.channels()-1; i++) {
maskDiffIdx.put(hh, ww,i,0);
maskDiffIdx2.put(hh, ww,i,100);
}

}
else{
maskDiffIdx2.put(hh, ww, maskDiffIdx3.get(hh, ww));
}
}


 :
I really need any one's help,
thanks in advance!!

ב-יום שני, 21 בנובמבר 2022 בשעה 15:03:05 UTC+2, Samuel Audet כתב/ה:

Samuel Audet

unread,
Nov 21, 2022, 7:51:43 PM11/21/22
to jav...@googlegroups.com, shira Jacobs
From which line exactly are you getting this error?

shira Jacobs

unread,
Nov 22, 2022, 2:11:35 AM11/22/22
to javacv
final UByteRawIndexer maskDiffIdx3 = m3.createIndexer()

ב-יום שלישי, 22 בנובמבר 2022 בשעה 02:51:43 UTC+2, Samuel Audet כתב/ה:

Samuel Audet

unread,
Nov 22, 2022, 2:22:04 AM11/22/22
to jav...@googlegroups.com, shira Jacobs
I see, you're getting this error from cvtColor(). That doesn't support
in-place conversion like that. You'll need to use another Mat object
than m3 for the "dst".

shira Jacobs

unread,
Nov 22, 2022, 3:41:01 AM11/22/22
to javacv
Hi Samuel
First thanks!! u were right  is not falling for me after I change the cvtconvrt() 

But the result from the above code Im getting the picture back colored and not gray 


maybe it returns to color when I put the pixel?
else{
maskDiffIdx2.put(hh, ww, maskDiffIdx3.get(hh, ww));
can you please take a look on my code and let me understand what did  I do wrong in my code??

appreciate it a lot!! 
thanks 
Shira.
ב-יום שלישי, 22 בנובמבר 2022 בשעה 09:22:04 UTC+2, Samuel Audet כתב/ה:

shira Jacobs

unread,
Nov 22, 2022, 5:31:26 AM11/22/22
to javacv
someone can help me???

ב-יום שני, 21 בנובמבר 2022 בשעה 13:21:09 UTC+2, ‪shira Jacobs‬‏ כתב/ה:

shira Jacobs

unread,
Nov 23, 2022, 11:47:44 PM11/23/22
to javacv
HI Samuel - 
You Wrote me above -

That's something we can easily do using indexers, yes

are you sure? I tried all different things but no, 
if I'm putting a Pixel in a GrayScale Mat it becomes non-color no matter what I put there,

and if Im putting the value of the GrayScale Mat in a new MAt I'm getting it in color 

what is the solution in such a case?
what I'm doing wrong? 

ב-יום שלישי, 22 בנובמבר 2022 בשעה 12:31:26 UTC+2, ‪shira Jacobs‬‏ כתב/ה:

Samuel Audet

unread,
Nov 24, 2022, 1:04:17 AM11/24/22
to jav...@googlegroups.com, shira Jacobs
A grayscale image cannot contain color pixels, that's normal. I don't
see any problems with that.

shira Jacobs

unread,
Nov 24, 2022, 1:51:46 AM11/24/22
to javacv
My question was if we can add some COLOR in specific  pixels in a grayScale MAT 

ב-יום חמישי, 24 בנובמבר 2022 בשעה 08:04:17 UTC+2, Samuel Audet כתב/ה:

Keith Hall

unread,
Nov 24, 2022, 3:55:31 AM11/24/22
to javacv
This will hopefully give you a clue why you can't color a grayscale image
A grayscale Mat 2000 pixels by 2000 has the properties
 org.bytedeco.opencv.opencv_core.Mat[width=2000,height=2000,depth=8,channels=1]
A color Mat of same dimensions
org.bytedeco.opencv.opencv_core.Mat[width=2000,height=2000,depth=8,channels=3]
You can convert a grayscale back to rgb color (it will still look gray) by using 
cvtColor(image,imagergb,CV_GRAY2RGB);
You can then use an indexer on your color version of the grayscale to set specific pixels to their 3 channel values
Reply all
Reply to author
Forward
0 new messages