how to change the background color of an image from black to white

3 views (last 30 days)
i want to change the background color of an image using matlab , i have uploaded an image also in this i want to change the background color of an 2nd image.please reply

Answers (1)

DGM
DGM on 20 Dec 2023
This is another lesson in how to make sure nobody bothers answering what might appear to be a simple question.
First, it should be fairly obvious that if you're asking "how do I do X to this image, file, or other data", it helps to include the relevant image, etc. -- not a screenshot or a cellphone photo of the thing. Not only does the question ask "how do I change this image in the screenshot", they ask to do the same thing to another completely undescribed image.
Second, it can help to explain what your actual goals are, not the next step that you think will fix whatever broken code you're keeping secret. For example, if we're just supposed to modify the second image without regard to any other information or image, then that's simple enough.
inpict = imread('resistorbk.png'); % an RGB image
mask = all(inpict==0,3); % a logical mask
outpict = inpict;
outpict(repmat(mask,[1 1 3])) = 255; % replace the masked pixels
imshow([inpict; outpict],'border','tight') % display both
That was simple enough, but what does it accomplish? Given that we can recognize the object content and can make some guesses as to the reasons why someone might be processing images of resistors, it's hard to imagine any scenario where this operation is actually productive.
We know that these color bands correspond to the bands in the other image, but they do not contain information about all of the bands. Changing the "background" of this image does not reveal the black band, because there is none. It's simply an indistinguishable region of the background.
If instead of editing the image itself, we're supposed to edit the routine which creates this image with the black background such that it creates an image with a white background, then what does that accomplish? Resistors have white bands too.
So we arrive at the big question. Why does this image even need to exist? We understand that it bears some information associated with the image on the left, but what's its role in the unknown workflow?
If the broader goal is to construct or modify synthetic images of resistors, such that the result is another image of a resistor (e.g. creating a set of sample images for decoder testing), then it's unlikely that either of these images would be useful in a composition without an additional mask. It's arguable that they might be usable in a masked composition operation (or compositing with blending), but:
  • any practical mask would make the background color irrelevant
  • unless you're using MIMT already, there aren't any of the sorts of blending tools I'm imagining could be useful here.
In other words, since we need the result to have a neutral response which excludes the background region from the output, and the color bands may potentially be black or white, then you can't rely on the neutral response of the blending operation on the color channels alone. You need an additional mask in some form.
If instead, the broader goal is indeed to decode the resistor image on the left, then creating and editing the image on the right is very unlikely to be anything other than a waste of time. All you're doing is duplicating information. If you've extracted enough information from the image on the left that you know what colors to use in the image on the right, then you're done. That's the information you need. You don't need an image of it. If the process of acquiring said information naturally produces the image on the right, then the question that was actually asked didn't make that clear, and the answer to said question doesn't actually make the image useful, does it?
Note now many implicit questions I'm asking. When the answer to your question is nothing but a bunch of questions, you might have done a poor job in asking your question to begin with.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!