How do I use cv2.xphoto.inpaint? - python

I have two images that I have uniquely named image and mask.
I use the following code to perform standard OpenCV inpainting:
image = cv2.imread('image.jpg')
mask= cv2.imread('mask.jpg')
dst = cv2.inpaint([1], mask, 3, cv2.INPAINT_NS)
I receive the results, and they look decent; however, I realized that there is another function of OpenCV called cv2.xphoto.inpaint (link: https://docs.opencv.org/4.x/dc/d2f/tutorial_xphoto_inpainting.html and https://docs.opencv.org/4.x/de/daa/group__xphoto.html)
Is the proper usage of this code as follows?
new_dst = cv2.xphoto.inpaint(image,mask, dst, cv2.xphoto.INPAINT_FSR_FAST)
If so, why does new_dst remain empty after running the previous line?

Related

Create a directory to store output mask, for image segmentation task

following the tutorial Kaggle Notebook for Unet, I am trying to create a function that could store the predicted mask in a folder. While trying below codes, I am getting error 'built-in function imread returned NULL without setting an error'
Please suggest a modification or redirect for potential solutions.
import cv2
import os
image_dir = "/content/sample_data/Output"
def pred_images(sample_image, sample_mask, path):
pred_mask = model.predict(sample_image[tf.newaxis, ...])
print(pred_mask.shape)
pred_mask = pred_mask.reshape(img_size[0],img_size[1],1)
img= cv2.imread(pred_mask, 1)
cv2.imwrite(os.path.join(path, '*.png'), img)
for i in range(len(train_dataset)):
for image, mask in TRAIN.take(i):
sample_image, sample_mask= image, mask
pred_images(sample_image, sample_mask, {image_dir})
You have several errors in your code:
You are using imread for what exactly? cv2.imread ismeant to open an image file and read the image into a variable. That is, to "convert" a string with filename into a matrix with the actual pixel values.
However, you are applying imread to a matrix mask_pred -- what are you doing there? This makes no sense, thus the error message you got.
You are trying to write your image, img to a file name '/content/sample_data/Output/*.png' -- this is NOT a valid file name. You are not allowed to use '*' (and a few other special characters) in file names.
Moreover, your path argument is set to {image_dir} -- that is, you are making a set with one element (image_dir) and then try and use this set as the path for the file.

remove a particular color from an image

Is it possible to remove a particular rbg color from an image? For example, I want to create a function such that I pass an image and color as a parameter and it returns the same image but without that color.
For example, this function
fuction(image, "R")
should give me an image that has no R shades. How can I do so?
Currently, something like this works:
def exclusionWithPIL(image, channel):
out = None
image = Image.open(image)
image_data = image.load()
height,width = image.size
for loop1 in range(height):
for loop2 in range(width):
r,g,b = image_data[loop1,loop2]
image_data[loop1,loop2] = 0,g,b
return image
result = rgb_exclusion('./image.jpg', "G")
result.save('new.jpg')
but here, I'm reading the image like this Image.open(image) inside the function. Instead, I want to pass in image1 which is already read like this:
image1 = load(image1_path)
def load(image_path):
out = io.imread(image_path)
out = out.astype(np.float64) / 255
return out
How can I modify the function accordingly?
P.S I use io.imread from skimage to read images.
Edit:
if I pass the image loaded by io.imread directly into the function, something like this seems to work:
out = image.copy()
if (channel == "R"):
out[:, :, 0] = 0
But I don't quite quite understand the indexes [:, :, 0]
found a tutorial online which solves exactly your problem:
Tutorial Link
The main idea behind their solution is to use PIL in order to open the image, load it, and then iterate over every pixel in the image (using 2 loops) and setting the chosen color to zero.
EDIT: I think that your problem occurs because of the fact that you're trying to pass to the function as a parameter an image which was loaded using sicikit-image library (io.imread()), and then inside the function you're trying to use PILLOW library's features on the same image.
That's why it shouldn't work, you're merging between 2 different libraries.
As I see it, there are 2 possible solution:
Make the function's parameter the image's path instead of the image itself, and then save the image in the same directory and do not return anything.
Stay consistent, and before passing the function an image as a parameter, load it using the PILLOW library, and when returning it, refer to the returned image in terms of the PILLOW library only.

python simple substitute variableć„“ (opencv image processing)

enter image description here
Why img is same result with img_gray?
I think img must be showed original image.
You have to duplicate image
img_gray = img.copy()
Without copy() both variables gives access to the same image in memory.
It is standard behavior in Python.

Insert a masked region of an image in another (HDR) via OpenCV

I have two images and a mask. The first image (im1) is my source image, the second (im2) is the image whose region need to be inserted in im1 and the third image (mask) contains 1's in the region that needs to be pasted. All images have the same size (H*W*3). It should be noted that im1 is HDR( .exr format).
After reading via OpenCV
im1 = .imread(im1, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)[:,:,0:3]
im2 = ...
mask = ...
how can I transfer the masked region(contained in mask array) of image im2 without any loss of information (no change apart from masked region) in im1?
Normally you would use OpenCV's copyTo() method which will copy an image or masked image region from one Mat to another.
Unfortunately, this functionality is not available in the OpenCV Python bindings.
There is a Python workaround for this function from this answer though which you could use instead.

Image segmentation using corresponding masks in python

I have corresponding masks to the images that I want to segment.
I put the images in one folder and their corresponding masks in another folder.
I'm trying to apply those masks or multiply them by the images using two for loops in python to get the segmented images.
I'm using the code below:
def ImageSegmentation():
SegmentedImages = []
for img_path in os.listdir('C:/Users/mab/Desktop/images/'):
img=io.imread('C:/Users/mab/Desktop/data/'+img_path)
for img_path2 in os.listdir('C:/Users/mab/Desktop/masks/'):
Mask = io.imread('C:/Users/mab/Desktop/masks/'+img_path2)
[indx, indy] = np.where(Mask==0)
Color_Masked = img.copy()
Color_Masked[indx,indy] = 0
matplotlib.image.imsave('C:/Users/mab/Desktop/SegmentedImages/'+img_path2,Color_Masked)
segs.append(Color_Masked)
return np.vstack(Color_Masked)
This code works when I try it for a single image and a single mask (without the folders and loops).
However, when I try to loop over the images and masks I have in the two folders, I get output images that are segmented by the wrong mask (not their corresponding mask).
I can't segment each single image alone without looping because I have more than 500 Images and their masks.
I don't know what I'm missing or placing wrong in this code and how can I fix it? Also, is there an easier way to get the segmented images?
Unless I have grossly misunderstood, you just need something like this:
import glob
filelist = glob.glob('C:/Users/mab/Desktop/images/*.png')
for i in filelist:
mask = i.replace("images","masks")
print(i,mask)
On my iMac, that sort of thing produces:
/Users/mark/StackOverflow/images/b.png /Users/mark/StackOverflow/masks/b.png
/Users/mark/StackOverflow/images/a.png /Users/mark/StackOverflow/masks/a.png

Categories

Resources