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:)
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)
Related
I'm learning opencv via the following link link link2. And I got error related to image training process for face recognition. Please correct me or help with the problem I'm having. thank you
System Information :
OpenCV 4.5.2.52
Python 3.9.5
Detailed Description
I need to train an images by using the code below and the results was :
- OpenCV/faces-train.py", line 50, in <module>
recognizer.train(x_train, np.array(y_labels))
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'train'
> Overload resolution failed:
> - src is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'src'
Code
import cv2
import os
import numpy as np
from PIL import Image
import pickle
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")
face_cascade = cv2.CascadeClassifier('cascade/data/haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
label_ids = {}
y_labels = []
x_train = []
for root, dirs, files, in os.walk(image_dir):
for file in files:
if file.endswith("png") or file.endswith("jpg") or file.endswith("jpeg") :
path = os.path.join(root, file)
label = os.path.basename(root).replace(" ", "-").lower()
# print(label, path)
if not label in label_ids:
label_ids[label] = current_id
current_id += 1
id_ = label_ids[label]
# print(label_ids)
y_labels.append(label)
x_train.append(path)
pil_image = Image.open(path).convert("L")
image_array = np.array(pil_image, "uint8")
# print(image_array)
faces = face_cascade.detectMultiScale(image_array, scaleFactor=1.5, minNeighbors=5)
for(x,y,w,h) in faces:
roi = image_array[y:y+h, x:x+w]
x_train.append(roi)
y_labels.append(id_)
# print(y_labels)
# print(x_train)
with open("labels.pickle", 'wb') as f:
pickle.dump(label_ids, f)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yml")
remove the following lines:
y_labels.append(label)
x_train.append(path)
you don't really need to append the labels or the paths since you are basically training the face recognizer to recognise/categorise faces. Likewise, you wouldnt need the path to your images as it is also not necessary. Guessing this is what caused the error!
Do let me know if the fix works! Cheers :)
I am trying to read and display DICOM(.dcm) images using below code:-
import pydicom as dicom
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
from PIL.ImageQt import ImageQt
def display_dicom_images(self, folder_Path):
try:
# Image parameters
image_width = 382
image_height = 382
image_depth = 3
self.total_images_in_folder = len(glob.glob1(folder_Path,"*"))
# Select the center image for display
self.current_image_number = round(self.total_images_in_folder / 2)
self.display_number = self.current_image_number
image_dtype = np.uint8
pixel_array = np.ndarray([self.total_images_in_folder, image_height, image_width, image_depth]).astype(image_dtype)
# load images here, once better MR images are acquired
for image_index in range(0, self.total_images_in_folder):
# for DICOM
image_path = folder_Path + "/" + str(image_index) + ".dcm"
scan_image = dicom.dcmread(image_path)
scan_image = scan_image.pixel_array.astype(image_dtype)
pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image
return pixel_array
But getting error:-
IndexError('tuple index out of range',)
i am using pillow python library for image.
How do you know scan_image.shape is of length 3? MR images should only be monochrome, which would make image_depth = 1 and the length of scan_image.shape equal to 2.
C.8.3.1.1.3 Photometric Interpretation
Enumerated Values:
MONOCHROME1
MONOCHROME2
I'm trying to make a panorama from a image set. I'm using Spyder, OpenCV 3.4 and Python 3.7. Here's the code:
The main:
from stitches import Stitcher
#from PIL import Image
import os
import glob
import numpy as np
import imutils
import cv2
cap = cv2.VideoCapture('C:/Users/VID_20181208_111037881.mp4')
img_dir = "C:/Users/user/images"
data_path = os.path.join(img_dir, '*g')
files = glob.glob(data_path)
args = []
for f1 in files:
img = cv2.imread(f1)
args.append(img)
def retImg(img):
return img
for i in args:
j = i+1
frame = retImg(i)
frame2 = retImg(j)
#imageA = cv2.imread(frame)
#imageB = cv2.imread(frame2)
imageA = imutils.resize(frame, width=400)
imageB = imutils.resize(frame2, width=400)
# stitch the images together to create a panorama
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=False)
frame = result
# show the images
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
The stitch inside stitches.py:
def stitch(self, images, ratio=0.75, reprojThresh=4.0, showMatches=False):
# unpack the images, then detect keypoints and extract
# local invariant descriptors from them
(imageB, imageA) = images
(kpsA, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# match features between the two images
M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
# if the match is None, then there aren't enough matched
# keypoints to create a panorama
if M is None:
return None
# together
(matches, H, status) = M
result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
# check to see if the keypoint matches should be visualized
if showMatches:
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# return a tuple of the stitched image and the
# visualization
return (result, vis)
# return the stitched image
return result
When I compile it, the following error is shown:
File "C:/Users/user/panorama.py", line 44, in <module>
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=False)
TypeError: cannot unpack non-iterable NoneType object
And I cannot understand why. I'm new to Python, so maybe this mistake is really simple, but I don't know what to do here. Thanks by advance!
How can I create a function that takes an image file (not image filename) in python. Simply, like the following:
FaceController.py
import cv2
from Computer_Vision import Face_Detector as FD
def detectface():
img = cv2.imread('DSC_1902.JPG')
FD.detect(img)
detectface()
Face_Detector.py
import cv2
def detect(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('messigray.png', img)
return img
Error:
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
You can pass a pointer pointing to the image instead of the image or the filename of the image
EDIT
def image_function(imagePointer):
#DO SOMETHING WITH THE IMAGE
#HERE IS THE IMAGE POINTER
image = open('your_image.png')
#CALLING THE FUNCTION
image_function(image)
Sorry, I don't know opencv so I can not help in your code :(
You can actually pass the image as a tensor.
with cv2.imread() and torch.
Which is easy, useful.
short answer:
load with cv2.imread()
transform to tensor with
img = torch.Tensor(img)/255.
That works for my application.
Yours might be a little different.
Code answer:
from Computer_Vision import Face_Detector as FD
def detectface():
import cv2
import torch
folder = r"This Folder/"
image_file = folder+"image.png"
# or
# file = r"image.png"
# image_file = os.path.join(folder, file)
img = imread(image_file)
img = torch.Tensor(img)/255. # THE KEY LINE HERE.
FDdetect(img):
"""Do stuff with object detection..."""
result =
return result
How could I convert a CGImageRef to PIL without saving the image to disk on osx?
I though about getting the raw pixel data from the CGImageRef and using Image.fromstring() to make the PIL image by doing
import mss
import Quartz.CoreGraphics as CG
from PIL import Image
mss = mss.MSSMac()
for i, monitor in enumerate(mss.enum_display_monitors(0)):
imageRef = mss.get_pixels(monitor)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
img.show()
but this doesn't give me the correct image.
This is the image I expect:
and this is the image I get in PIL:
The screencapture from CG doesn't necessarily use the RGB colorspace. It may use RGBA or something else. Try changing:
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
to
img = Image.fromstring("RGBA", (monitor[b'width'], monitor[b'height']), pixeldata)
Here is how I detect which colorspace is actually being captured:
bpp = CG.CGImageGetBitsPerPixel(imageRef)
info = CG.CGImageGetBitmapInfo(imageRef)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = None
if bpp == 32:
alphaInfo = info & CG.kCGBitmapAlphaInfoMask
if alphaInfo == CG.kCGImageAlphaPremultipliedFirst or alphaInfo == CG.kCGImageAlphaFirst or alphaInfo == CG.kCGImageAlphaNoneSkipFirst:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata, "raw", "BGRA")
else:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)
elif bpp == 24:
img = Image.fromstring("RGB", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)
It was a bug I fixed some time ago. Here is how to achieve what you want using the latest mss version (2.0.22):
from mss.darwin import MSS
from PIL import Image
with MSS() as mss:
for monitor in mss.enum_display_monitors(0):
pixeldata = mss.get_pixels(monitor)
img = Image.frombytes('RGB', (mss.width, mss.height), pixeldata)
img.show()
Note that pixeldata is just a reference to mss.image, you can use it directly.