facebookresearch / maskrcnn-benchmark

Fast, modular reference implementation of Instance Segmentation and Object Detection algorithms in PyTorch.
MIT License
9.29k stars 2.5k forks source link

ValueError: not enough values to unpack (expected 3, got 2) #1127

Open MarioTiara opened 4 years ago

MarioTiara commented 4 years ago

So, I have try to run python program for vesselsblood segmentation form fundus image. I got some error like following

Traceback (most recent call last): File "C:\Users\mariotiara\Downloads\microaneurysm_detection-master\bloodvessels.py", line 55, in bloodvesssel=extract_bv(fundus) File "C:\Users\mariotiara\Downloads\microaneurysm_detection-master\bloodvessels.py", line 25, in extract_bv im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) ValueError: not enough values to unpack (expected 3, got 2)

it's the code

import cv2 import numpy as np import os import csv def extract_bv(image):
b,green_fundus,r = cv2.split(image) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) contrast_enhanced_green_fundus = clahe.apply(green_fundus)

# applying alternate sequential filtering (3 times closing opening)
r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)    
f4 = cv2.subtract(R3,contrast_enhanced_green_fundus)
f5 = clahe.apply(f4)        

# removing very small contours through area parameter noise removal
ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY) 
mask = np.ones(f5.shape[:2], dtype="uint8") * 255   
im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if cv2.contourArea(cnt) <= 200:
        cv2.drawContours(mask, [cnt], -1, 0, -1)            
im = cv2.bitwise_and(f5, f5, mask=mask)
ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV)            
newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)   

# removing blobs of unwanted bigger chunks taking in consideration they are not straight lines like blood
#vessels and also in an interval of area
fundus_eroded = cv2.bitwise_not(newfin) 
xmask = np.ones(fundus.shape[:2], dtype="uint8") * 255
x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)    
for cnt in xcontours:
    shape = "unidentified"
    peri = cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, 0.04 * peri, False)                  
    if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100:
        shape = "circle"    
    else:
        shape = "veins"
    if(shape=="circle"):
        cv2.drawContours(xmask, [cnt], -1, 0, -1)   

finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask)  
blood_vessels = cv2.bitwise_not(finimage)
return blood_vessels

if name == "main":
fundus = cv2.imread("22.jpg") bloodvesssel=extract_bv(fundus) cv2.imwrite("bloodssels.png", bloodvesssel)

please help me to found problm solve.

Sanjeeth8733 commented 4 years ago

Try this contours, hierarchy = cv2.findContours(f6.copy() ,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)

Quratulann23 commented 4 years ago

Sir I tried this but it gives me this following error: NameError: name 'f6' is not defined

Try this contours, hierarchy = cv2.findContours(f6.copy() ,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)

NanAlbert commented 4 years ago

You need the old version of OpenCV. Try this "pip install opencv-python==3.4.3.18"

neeraj2296 commented 4 years ago

@NanAlbert for pip installing with 3.4.18 i am getting this ERROR: Could not find a version that satisfies the requirement opencv-python==3.4.3.18 (from versions: 3.4.8.29, 3.4.9.31, 3.4.9.33, 3.4.10.35, 3.4.10.37, 3.4.11.39, 3.4.11.41, 4.1.2.30, 4.2.0.32, 4.2.0.34, 4.3.0.36, 4.3.0.38, 4.4.0.40, 4.4.0.42) ERROR: No matching distribution found for opencv-python==3.4.3.18

NanAlbert commented 4 years ago

@neeraj2296 The reason may be the source. You can try this: "pip install -i https://pypi.python.org/simple/ opencv-python==3.4.3.18"

Vanshika36 commented 2 years ago

import cv2 import numpy as np import os import csv

def extract_bv(image): b, green_fundus, r = cv2.split(image) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) contrast_enhanced_green_fundus = clahe.apply(green_fundus)

# applying alternate sequential filtering (3 times closing opening)
r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN,
                      cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)), iterations=1)
R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)), iterations=1)
r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)), iterations=1)
R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)), iterations=1)
r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (23, 23)), iterations=1)
R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (23, 23)), iterations=1)
f4 = cv2.subtract(R3, contrast_enhanced_green_fundus)
f5 = clahe.apply(f4)

# removing very small contours through area parameter noise removal
ret, f6 = cv2.threshold(f5, 15, 255, cv2.THRESH_BINARY)
mask = np.ones(f5.shape[:2], dtype="uint8") * 255
im2, contours, hierarchy = cv2.findContours(f6.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if cv2.contourArea(cnt) <= 200:
        cv2.drawContours(mask, [cnt], -1, 0, -1)
im = cv2.bitwise_and(f5, f5, mask=mask)
ret, fin = cv2.threshold(im, 15, 255, cv2.THRESH_BINARY_INV)
newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=1)

# removing blobs of unwanted bigger chunks taking in consideration they are not straight lines like blood
# vessels and also in an interval of area
fundus_eroded = cv2.bitwise_not(newfin)
xmask = np.ones(fundus.shape[:2], dtype="uint8") * 255
x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in xcontours:
    shape = "unidentified"
    peri = cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, 0.04 * peri, False)
    if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100:
        shape = "circle"
    else:
        shape = "veins"
    if (shape == "circle"):
        cv2.drawContours(xmask, [cnt], -1, 0, -1)

finimage = cv2.bitwise_and(fundus_eroded, fundus_eroded, mask=xmask)
blood_vessels = cv2.bitwise_not(finimage)
return blood_vessels

if name == "main": pathFolder = "C:/Users/This PC/PycharmProjects/first" filesArray = [x for x in os.listdir(pathFolder) if os.path.isfile(os.path.join(pathFolder, x))] destinationFolder = "C:/Users/This PC/PycharmProjects/first" if not os.path.exists(destinationFolder): os.mkdir(destinationFolder) for file_name in filesArray: file_name_no_extension = os.path.splitext(file_name)[0] fundus = cv2.imread(pathFolder + '23.jpg' + file_name) bloodvessel = extract_bv(fundus) cv2.imwrite(destinationFolder + file_name_no_extension + "23_MB.png", bloodvessel)

### same error with me please help!! File "C:\Users\This PC\PycharmProjects\first\bloodvessel.py", line 64, in bloodvessel = extract_bv(fundus) File "C:\Users\This PC\PycharmProjects\first\bloodvessel.py", line 8, in extract_bv b, green_fundus, r = cv2.split(image) ValueError: not enough values to unpack (expected 3, got 0)