How to change value of all pixels in cv::Mat










0















I have a simple function that attempts to loop through all the pixels in a single channel cv::Mat. Its not functioning correctly. I'm running this in xcode on ios sim.



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y!= h; y++)
for(int x = 0; x!= w; x++)
cv::Point2i p(y, x);
floated.at<int>(p) = currentColor;


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;



For some reason it prints a striped image.



enter image description hereenter image description here



Here is what the output of the cv::Mat looks like before the function returns



[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, ...









share|improve this question






















  • Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

    – Vinícius Queiroz
    Nov 16 '18 at 1:06











  • Yes, I edited the question. It's defined right before the pixel changes value.

    – Cristian
    Nov 16 '18 at 1:10















0















I have a simple function that attempts to loop through all the pixels in a single channel cv::Mat. Its not functioning correctly. I'm running this in xcode on ios sim.



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y!= h; y++)
for(int x = 0; x!= w; x++)
cv::Point2i p(y, x);
floated.at<int>(p) = currentColor;


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;



For some reason it prints a striped image.



enter image description hereenter image description here



Here is what the output of the cv::Mat looks like before the function returns



[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, ...









share|improve this question






















  • Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

    – Vinícius Queiroz
    Nov 16 '18 at 1:06











  • Yes, I edited the question. It's defined right before the pixel changes value.

    – Cristian
    Nov 16 '18 at 1:10













0












0








0








I have a simple function that attempts to loop through all the pixels in a single channel cv::Mat. Its not functioning correctly. I'm running this in xcode on ios sim.



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y!= h; y++)
for(int x = 0; x!= w; x++)
cv::Point2i p(y, x);
floated.at<int>(p) = currentColor;


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;



For some reason it prints a striped image.



enter image description hereenter image description here



Here is what the output of the cv::Mat looks like before the function returns



[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, ...









share|improve this question














I have a simple function that attempts to loop through all the pixels in a single channel cv::Mat. Its not functioning correctly. I'm running this in xcode on ios sim.



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y!= h; y++)
for(int x = 0; x!= w; x++)
cv::Point2i p(y, x);
floated.at<int>(p) = currentColor;


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;



For some reason it prints a striped image.



enter image description hereenter image description here



Here is what the output of the cv::Mat looks like before the function returns



[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, ...






c++ ios xcode opencv matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:54









Cristian Cristian

90115




90115












  • Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

    – Vinícius Queiroz
    Nov 16 '18 at 1:06











  • Yes, I edited the question. It's defined right before the pixel changes value.

    – Cristian
    Nov 16 '18 at 1:10

















  • Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

    – Vinícius Queiroz
    Nov 16 '18 at 1:06











  • Yes, I edited the question. It's defined right before the pixel changes value.

    – Cristian
    Nov 16 '18 at 1:10
















Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

– Vinícius Queiroz
Nov 16 '18 at 1:06





Is p defined somewhere else? Shouldn't it be defined as cv:Point2i p = cv:Point2i(y,x)?

– Vinícius Queiroz
Nov 16 '18 at 1:06













Yes, I edited the question. It's defined right before the pixel changes value.

– Cristian
Nov 16 '18 at 1:10





Yes, I edited the question. It's defined right before the pixel changes value.

– Cristian
Nov 16 '18 at 1:10












2 Answers
2






active

oldest

votes


















0














Ok, I changed your for loops condition check, changed the definition of your Point2i and also changed the template type of your at method from int to float, to maintain type integrity.



This should work now:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
cv:Point2i p = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;






share|improve this answer




















  • 1





    p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

    – Micka
    Nov 16 '18 at 6:47











  • Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

    – Vinícius Queiroz
    Nov 17 '18 at 19:44







  • 1





    no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

    – Micka
    Nov 17 '18 at 19:57







  • 1





    Oops, you're right! Thanks for the info!! Updated again!

    – Vinícius Queiroz
    Nov 17 '18 at 20:54


















1














You should use setTo:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;






share|improve this answer























  • This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

    – Vinícius Queiroz
    Nov 17 '18 at 19:46











  • @vini yes, of course. But the question explicitly asks for "all" pixels

    – Miki
    Nov 17 '18 at 23:23










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329950%2fhow-to-change-value-of-all-pixels-in-cvmat%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Ok, I changed your for loops condition check, changed the definition of your Point2i and also changed the template type of your at method from int to float, to maintain type integrity.



This should work now:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
cv:Point2i p = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;






share|improve this answer




















  • 1





    p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

    – Micka
    Nov 16 '18 at 6:47











  • Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

    – Vinícius Queiroz
    Nov 17 '18 at 19:44







  • 1





    no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

    – Micka
    Nov 17 '18 at 19:57







  • 1





    Oops, you're right! Thanks for the info!! Updated again!

    – Vinícius Queiroz
    Nov 17 '18 at 20:54















0














Ok, I changed your for loops condition check, changed the definition of your Point2i and also changed the template type of your at method from int to float, to maintain type integrity.



This should work now:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
cv:Point2i p = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;






share|improve this answer




















  • 1





    p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

    – Micka
    Nov 16 '18 at 6:47











  • Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

    – Vinícius Queiroz
    Nov 17 '18 at 19:44







  • 1





    no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

    – Micka
    Nov 17 '18 at 19:57







  • 1





    Oops, you're right! Thanks for the info!! Updated again!

    – Vinícius Queiroz
    Nov 17 '18 at 20:54













0












0








0







Ok, I changed your for loops condition check, changed the definition of your Point2i and also changed the template type of your at method from int to float, to maintain type integrity.



This should work now:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
cv:Point2i p = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;






share|improve this answer















Ok, I changed your for loops condition check, changed the definition of your Point2i and also changed the template type of your at method from int to float, to maintain type integrity.



This should work now:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;

int count = 1;

for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
cv:Point2i p = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)


std::cout << floated.channels() << std::endl;
// prints 1


std::cout << floated << std::endl;
return floated;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 20:56

























answered Nov 16 '18 at 1:28









Vinícius QueirozVinícius Queiroz

119110




119110







  • 1





    p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

    – Micka
    Nov 16 '18 at 6:47











  • Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

    – Vinícius Queiroz
    Nov 17 '18 at 19:44







  • 1





    no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

    – Micka
    Nov 17 '18 at 19:57







  • 1





    Oops, you're right! Thanks for the info!! Updated again!

    – Vinícius Queiroz
    Nov 17 '18 at 20:54












  • 1





    p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

    – Micka
    Nov 16 '18 at 6:47











  • Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

    – Vinícius Queiroz
    Nov 17 '18 at 19:44







  • 1





    no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

    – Micka
    Nov 17 '18 at 19:57







  • 1





    Oops, you're right! Thanks for the info!! Updated again!

    – Vinícius Queiroz
    Nov 17 '18 at 20:54







1




1





p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

– Micka
Nov 16 '18 at 6:47





p is wrong, it should be (x,y) order. Then you could use floated.at<float>(p), too. But with direct indexing it is in the right ordering floated.at<float>(y,x) which is usable, too.

– Micka
Nov 16 '18 at 6:47













Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

– Vinícius Queiroz
Nov 17 '18 at 19:44






Actually, in this case, declaration of pis useless. I put it that way just to denote how it should be declared in the first place, and then used direct indexing to show another way of using .at, not using points. But p should be declared as (y,x), as y (declared in the first loop) is ranging vertically (with height, not width), and the Point2i declaration is (row, column). Anyways, I'll update the answer.

– Vinícius Queiroz
Nov 17 '18 at 19:44





1




1





no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

– Micka
Nov 17 '18 at 19:57






no, points are (x=col,y=row) and have (x,y) ordering in the constructor. that's what I wanted to tell you. See stackoverflow.com/q/25642532/2393191

– Micka
Nov 17 '18 at 19:57





1




1





Oops, you're right! Thanks for the info!! Updated again!

– Vinícius Queiroz
Nov 17 '18 at 20:54





Oops, you're right! Thanks for the info!! Updated again!

– Vinícius Queiroz
Nov 17 '18 at 20:54













1














You should use setTo:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;






share|improve this answer























  • This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

    – Vinícius Queiroz
    Nov 17 '18 at 19:46











  • @vini yes, of course. But the question explicitly asks for "all" pixels

    – Miki
    Nov 17 '18 at 23:23















1














You should use setTo:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;






share|improve this answer























  • This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

    – Vinícius Queiroz
    Nov 17 '18 at 19:46











  • @vini yes, of course. But the question explicitly asks for "all" pixels

    – Miki
    Nov 17 '18 at 23:23













1












1








1







You should use setTo:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;






share|improve this answer













You should use setTo:



cv::Mat fillEdge(cv::Mat floated) 
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 9:37









MikiMiki

30.5k854141




30.5k854141












  • This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

    – Vinícius Queiroz
    Nov 17 '18 at 19:46











  • @vini yes, of course. But the question explicitly asks for "all" pixels

    – Miki
    Nov 17 '18 at 23:23

















  • This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

    – Vinícius Queiroz
    Nov 17 '18 at 19:46











  • @vini yes, of course. But the question explicitly asks for "all" pixels

    – Miki
    Nov 17 '18 at 23:23
















This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

– Vinícius Queiroz
Nov 17 '18 at 19:46





This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add an if statement in the inner for loop to apply a new color only to the edges (as the name of the function suggests), then the .at() method should be used.

– Vinícius Queiroz
Nov 17 '18 at 19:46













@vini yes, of course. But the question explicitly asks for "all" pixels

– Miki
Nov 17 '18 at 23:23





@vini yes, of course. But the question explicitly asks for "all" pixels

– Miki
Nov 17 '18 at 23:23

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329950%2fhow-to-change-value-of-all-pixels-in-cvmat%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin