Assign different colors to different biological cells in an image












0














I need to assign different colors to different biological cells in an image.



To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.





Edit: It is biological cells. I have found something similar online:



biological cells



Source: http://brainiac2.mit.edu/isbi_challenge/










share|improve this question
























  • Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
    – Mark Setchell
    Nov 19 '18 at 17:54










  • It is biological cells. I have just edited my question.
    – NeverBe
    Nov 19 '18 at 18:44






  • 1




    Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
    – NeverBe
    Nov 19 '18 at 19:40


















0














I need to assign different colors to different biological cells in an image.



To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.





Edit: It is biological cells. I have found something similar online:



biological cells



Source: http://brainiac2.mit.edu/isbi_challenge/










share|improve this question
























  • Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
    – Mark Setchell
    Nov 19 '18 at 17:54










  • It is biological cells. I have just edited my question.
    – NeverBe
    Nov 19 '18 at 18:44






  • 1




    Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
    – NeverBe
    Nov 19 '18 at 19:40
















0












0








0


0





I need to assign different colors to different biological cells in an image.



To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.





Edit: It is biological cells. I have found something similar online:



biological cells



Source: http://brainiac2.mit.edu/isbi_challenge/










share|improve this question















I need to assign different colors to different biological cells in an image.



To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.





Edit: It is biological cells. I have found something similar online:



biological cells



Source: http://brainiac2.mit.edu/isbi_challenge/







python image opencv image-processing cv2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:52









Dan Mašek

8,84932546




8,84932546










asked Nov 19 '18 at 14:59









NeverBeNeverBe

3417




3417












  • Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
    – Mark Setchell
    Nov 19 '18 at 17:54










  • It is biological cells. I have just edited my question.
    – NeverBe
    Nov 19 '18 at 18:44






  • 1




    Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
    – NeverBe
    Nov 19 '18 at 19:40




















  • Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
    – Mark Setchell
    Nov 19 '18 at 17:54










  • It is biological cells. I have just edited my question.
    – NeverBe
    Nov 19 '18 at 18:44






  • 1




    Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
    – NeverBe
    Nov 19 '18 at 19:40


















Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54




Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54












It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44




It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44




1




1




Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40






Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40














1 Answer
1






active

oldest

votes


















4














A simple approach comes to mind:




  1. Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.



  2. Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling cv2.threshold with the flag RETR_CCOMP:




    At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.




    N.B.: "Components" are the white parts of image, "holes" are the black parts.



  3. Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.





Sample Code:



import cv2
import numpy as np

img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)

cv2.imwrite('cells_colour.png', output)


Result:








share|improve this answer





















  • Thank you! This is what I am looking for.
    – NeverBe
    Nov 19 '18 at 19:38










  • Excellent solution!
    – Mark Setchell
    Nov 20 '18 at 10:01











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%2f53377311%2fassign-different-colors-to-different-biological-cells-in-an-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














A simple approach comes to mind:




  1. Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.



  2. Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling cv2.threshold with the flag RETR_CCOMP:




    At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.




    N.B.: "Components" are the white parts of image, "holes" are the black parts.



  3. Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.





Sample Code:



import cv2
import numpy as np

img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)

cv2.imwrite('cells_colour.png', output)


Result:








share|improve this answer





















  • Thank you! This is what I am looking for.
    – NeverBe
    Nov 19 '18 at 19:38










  • Excellent solution!
    – Mark Setchell
    Nov 20 '18 at 10:01
















4














A simple approach comes to mind:




  1. Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.



  2. Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling cv2.threshold with the flag RETR_CCOMP:




    At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.




    N.B.: "Components" are the white parts of image, "holes" are the black parts.



  3. Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.





Sample Code:



import cv2
import numpy as np

img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)

cv2.imwrite('cells_colour.png', output)


Result:








share|improve this answer





















  • Thank you! This is what I am looking for.
    – NeverBe
    Nov 19 '18 at 19:38










  • Excellent solution!
    – Mark Setchell
    Nov 20 '18 at 10:01














4












4








4






A simple approach comes to mind:




  1. Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.



  2. Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling cv2.threshold with the flag RETR_CCOMP:




    At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.




    N.B.: "Components" are the white parts of image, "holes" are the black parts.



  3. Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.





Sample Code:



import cv2
import numpy as np

img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)

cv2.imwrite('cells_colour.png', output)


Result:








share|improve this answer












A simple approach comes to mind:




  1. Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.



  2. Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling cv2.threshold with the flag RETR_CCOMP:




    At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.




    N.B.: "Components" are the white parts of image, "holes" are the black parts.



  3. Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.





Sample Code:



import cv2
import numpy as np

img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)

cv2.imwrite('cells_colour.png', output)


Result:









share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:36









Dan MašekDan Mašek

8,84932546




8,84932546












  • Thank you! This is what I am looking for.
    – NeverBe
    Nov 19 '18 at 19:38










  • Excellent solution!
    – Mark Setchell
    Nov 20 '18 at 10:01


















  • Thank you! This is what I am looking for.
    – NeverBe
    Nov 19 '18 at 19:38










  • Excellent solution!
    – Mark Setchell
    Nov 20 '18 at 10:01
















Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38




Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38












Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01




Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53377311%2fassign-different-colors-to-different-biological-cells-in-an-image%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith