Reputation: 61
Need assistance with the simple task. I’m playing around with the LISC dataset that contains hematological images taken from peripheral blood and segmentation masks of manual ground truth for these graphical samples. The task is the following:
Results should be like this (these were obtained via a combination of Mask R-CNN, GrabCut, and OpenCV — but not suitable for the current project I’m working on):
Here is the code that I’ve got so far (from jupyter notebook):
%matplotlib inline
from sklearn.datasets import load_sample_images
from matplotlib import pyplot as plt
from matplotlib.pyplot import imread
import cv2
original = imread(r'D:\mask\1_original.bmp')
plt.imshow(original)
segmented = imread(r'D:\mask\1_mask.bmp', cv2.IMREAD_GRAYSCALE)
_, mask = cv2.threshold(segmented, thresh=180, maxval=255, type=cv2.THRESH_BINARY)
plt.imshow(segmented)
I don't know why the image changed its colors since the original mask looks like this (just black and gray):
Here is the code that I'm using for the final result, but colors are distorted and different from the original once the mask is applied, and this is what I'm trying to fix now but to no avail:
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
import cv2
img_org = cv2.imread(r'D:\mask\1_original.bmp')
img_mask = cv2.imread(r'D:\mask\1_mask.bmp')
##Resizing images
img_org = cv2.resize(img_org, (400,400), interpolation = cv2.INTER_AREA)
img_mask = cv2.resize(img_mask, (400,400), interpolation = cv2.INTER_AREA)
for h in range(len(img_mask)):
for w in range(len(img_mask)):
if img_mask[h][w][0] == 0:
for i in range(3):
img_org[h][w][i] = 0
else:
continue
plt.imshow(img_org)
Thanks in advance!
Upvotes: 1
Views: 5000
Reputation: 3958
The change in colors is the result of the specified heatmap (viridis instead of binary) as noted above in comments.
The output image has different coloration than the input image because OpenCV uses BGR rather than RGB for colors, so it's likely your red and blue channels are swapped. If you read an image with OpenCV and plot with Matplotlib or vice versa. There are two easy solutions:
1.) Both read and plot images with OpenCV. You can replace plt.imshow(im_orig)
with:
cv2.imshow("window name", im_orig)
cv2.waitKey(0) # waits until a key is pressed before continuing
cv2.destroyAllWindows()
2.) Swap the R and B channels before plotting with plt.imshow()
:
im_orig = cv2.cvtColor(im_orig, cv2.COLOR_BGR2RGB)
plt.imshow(im_orig)
Upvotes: 1