2

Hello stackoverflow people:) I'm trying to masking many image from two different path, but I don't have an idea to do that. This an example for just two images and what I've do so far

image = cv.imread('Dataset/IDRiD_02.jpg', cv.IMREAD_COLOR)
od = cv.imread('od/IDRiD_02_OD.jpg', cv.IMREAD_GRAYSCALE)
mask = od
other = cv.bitwise_not(mask)
masking =  cv.bitwise_and(image, image, mask=other)
cv.imwrite('Output/masking/' + 'masking.jpg', masking)

Input is IDRiD_02.jpg and IDRiD_02_OD.jpg then Output is masking.jpg

Then I want to do the same but with many images

import cv2 as cv
import numpy as np
import os
import glob
import os.path

od_images = [] 

for directory_path in glob.glob("od/"):
    for mask_path in glob.glob(os.path.join(directory_path, "*.jpg")):
        mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
        od_images.append(mask)       
od_images = np.array(od_images)

path = "Dataset/*.jpg"

for file in glob.glob(path):
        
    #read image
    image = cv.imread(file, cv.IMREAD_COLOR)
    
    # e.g. MyPhoto.jpg
    basename = os.path.basename(file)
    # e.g. MyPhoto
    name = os.path.splitext(basename)[0]
    
    mask = cv.bitwise_not(od_images)
    
    masking =  cv.bitwise_and(image, image, mask = mask)
    
    cv.imwrite('Output/masking/' + name + '_masking.jpg', masking)

but then after I run the code, I'm getting the following error message

masking =  cv.bitwise_and(image, image, mask = mask)

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'

anyone can understand and help me? Thank you before:)

  • please print `.dtype` and `.shape` of `image` and `mask` in that loop, right before the error happens. add this info to your post. -- your pictures are probably of different sizes. you need to pad/crop that mask to match. – Christoph Rackwitz Mar 29 '22 at 09:46
  • @ChristophRackwitz This `.dtype` and `.shape` of image and mask. I don't know why mask have 81 channel similiar as according to the number of picture `Image Detail : Image Name: IDRiD_01 Image Size: 36636672 Image Shape: (2848, 4288, 3) Image Dtype: uint8 Mask Detail : Mask Name: IDRiD_01 Mask Size: 989190144 Mask Shape: (81, 2848, 4288) Mask Dtype: uint8` – Kadek Angga Mar 29 '22 at 12:14
  • Hi brother, check this link, hope it will work https://note.nkmk.me/en/python-opencv-numpy-alpha-blend-mask/ – Parthiban Marimuthu Mar 29 '22 at 12:26
  • do you even know what you want to achieve by putting ALL (mask?) images into `od_images`? – Christoph Rackwitz Mar 29 '22 at 13:07
  • @ChristophRackwitz I mean, I need to masking each image instead doing one by one like my first code `image = cv.imread('Dataset/IDRiD_02.jpg', cv.IMREAD_COLOR) od = cv.imread('od/IDRiD_02_OD.jpg', cv.IMREAD_GRAYSCALE) mask = od other = cv.bitwise_not(mask) masking = cv.bitwise_and(image, image, mask=other) cv.imwrite('Output/masking/' + 'masking.jpg', masking)` Instead I'm doing on by one, I want to loop it. Did you understand what I mean? I'm so sorry my english is really bad. Hope you understand what I mean – Kadek Angga Mar 30 '22 at 11:16
  • @ParthibanMarimuthu Yes I understand the link you share, but I have many images and I want to loop for each image instead I'm doing one by one brother:) – Kadek Angga Mar 30 '22 at 11:18

1 Answers1

1

Hope it will work for you !

import cv2 as cv
import os

img_path = r"image_folder_path"
od_images = r"od_img_folder_path"
for img,od in zip(os.listdir(img_path), os.listdir(od_images)):

    image = cv.imread(img_path+"\\"+img, cv.IMREAD_COLOR)
    od = cv.imread(od_images+"\\"+od, cv.IMREAD_GRAYSCALE)

    other = cv.bitwise_not(od)
    res =  cv.bitwise_and(image, image, mask=other)

     cv.imwrite('Output/masking/' +img+ '_masking.jpg', res)
Parthiban Marimuthu
  • 665
  • 1
  • 8
  • 15
  • Thank you so much! This what I meann:D You need little edit in this line `cv.imwrite('Output/masking/' +img+ '_masking.jpg', masking)` to `cv.imwrite('Output/masking/' +img+ '_masking.jpg', res)` because the variable you use in `cv.bitwise_and` is `res` not `masking` but yeah again thank you so much! – Kadek Angga Mar 31 '22 at 02:39