How to change value of all pixels in cv::Mat
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.
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
add a comment |
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.
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
Isp
defined somewhere else? Shouldn't it be defined ascv: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
add a comment |
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.
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
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.
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
c++ ios xcode opencv matrix
asked Nov 16 '18 at 0:54
Cristian Cristian
90115
90115
Isp
defined somewhere else? Shouldn't it be defined ascv: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
add a comment |
Isp
defined somewhere else? Shouldn't it be defined ascv: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
add a comment |
2 Answers
2
active
oldest
votes
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;
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 ofp
is 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. Butp
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
add a comment |
You should use setTo
:
cv::Mat fillEdge(cv::Mat floated)
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add anif
statement in the innerfor
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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 ofp
is 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. Butp
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
add a comment |
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;
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 ofp
is 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. Butp
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
add a comment |
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;
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;
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 ofp
is 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. Butp
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
add a comment |
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 ofp
is 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. Butp
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
p
is 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
p
is 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
add a comment |
You should use setTo
:
cv::Mat fillEdge(cv::Mat floated)
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add anif
statement in the innerfor
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
add a comment |
You should use setTo
:
cv::Mat fillEdge(cv::Mat floated)
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add anif
statement in the innerfor
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
add a comment |
You should use setTo
:
cv::Mat fillEdge(cv::Mat floated)
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
You should use setTo
:
cv::Mat fillEdge(cv::Mat floated)
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
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 anif
statement in the innerfor
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
add a comment |
This will surely change the value of all pixels, but we lose control over each independent pixel. If we want to add anif
statement in the innerfor
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Is
p
defined somewhere else? Shouldn't it be defined ascv: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