Esempio n. 1
0
def mix_brock():
    img_src1 = cv.imread("./img/1.png", 1)
    img_src2 = cv.imread("./img/2.png", 1)
    img_src3 = cv.imread("./img/3.png", 1)

    try:
        pal = cv.getTrackbarPos('hoge', winName)
    except:
        pal = 150

    img_ave = img_src1 * (1 / 3) + img_src2 * (1 / 3) + img_src3 * (
        1 / 3) + pal - 150
    cv.imwrite("./img/mix.png", img_ave)
    img = Image.open('./img/mix.png')

    to_pil(cca.grey_world(from_pil(to_pil(cca.stretch(
        from_pil(img)))))).save('./img/block.png')
    try:
        pal2 = cv.getTrackbarPos('hige', winName)
    except:
        pal2 = 0

    if pal2 == 0:
        to_pil(cca.grey_world(from_pil(to_pil(cca.stretch(
            from_pil(img)))))).save('./img/block.png')
    elif pal2 == 1:
        to_pil(cca.retinex_with_adjust(cca.retinex(
            from_pil(img)))).save('./img/block.png')
    elif pal2 == 2:
        to_pil(cca.stretch(from_pil(img))).save('./img/block.png')
Esempio n. 2
0
def autoWhiteBalance(fl_input, fl_output, method = "automatic"):
    print("imageProcessing::autoWhiteBalance(fl_input, fl_output, method = 'automatic'")
    
    try:
        # Open the image object to be adjusted.
        img_pil_input = Image.open(fl_input)
        
        # Define the empty image object.
        img_pil_adj = None
        
        if method == "stretch": img_pil_adj = to_pil(cca.stretch(from_pil(img_pil_input)))
        elif method == "gray_world": img_pil_adj = to_pil(cca.gray_world(from_pil(img_pil_input)))
        elif method == "max_white": img_pil_adj = to_pil(cca.max_white(from_pil(img_pil_input)))
        elif method == "retinex": img_pil_adj = to_pil(cca.cca.retinex(from_pil(img_pil_input)))
        elif method == "retinex_adjusted": img_pil_adj = to_pil(cca.retinex_with_adjust(from_pil(img_pil_input)))
        elif method == "stdev_luminance": img_pil_adj = to_pil(cca.standard_deviation_and_luminance_weighted_gray_world(from_pil(img_pil_input)))
        elif method == "stdev_grey_world": img_pil_adj = to_pil(cca.standard_deviation_weighted_grey_world(from_pil(img_pil_input)))
        elif method == "luminance_weighted": img_pil_adj = to_pil(cca.luminance_weighted_gray_world(from_pil(img_pil_input)))
        elif method == "automatic": img_pil_adj = to_pil(cca.automatic_color_equalization(from_pil(img_pil_input)))
        
        # Save the adjusted image.
        img_pil_adj.save(fl_output)
        
        # Return the output file name.
        return(fl_output)
    except Exception as e:
        print("Error occured in imageProcessing::autoWhiteBalance(fl_input, fl_output, method = 'automatic'")
        print(str(e))
        error.ErrorMessageImageProcessing(details=str(e), show=True, language="en")
        return(None)
Esempio n. 3
0
    def stretch_eq(self, img):
        # convert to pil format
        img_pil = self.opencv_to_pil(img)
        img_stretch_pil = to_pil(cca.stretch(from_pil(img_pil)))
        img_stretch_opencv = cv2.cvtColor(np.array(img_stretch_pil),
                                          cv2.COLOR_RGB2BGR)

        return img_stretch_opencv
Esempio n. 4
0
 def test_all(self):
     to_pil(stretch(from_pil(self.img)))
     to_pil(grey_world(from_pil(self.img)))
     to_pil(retinex(from_pil(self.img)))
     to_pil(max_white(from_pil(self.img)))
     to_pil(retinex_with_adjust(retinex(from_pil(self.img))))
     to_pil(
         standard_deviation_weighted_grey_world(from_pil(self.img), 20, 20))
     to_pil(
         standard_deviation_and_luminance_weighted_gray_world(
             from_pil(self.img), 20, 20))
     to_pil(luminance_weighted_gray_world(from_pil(self.img), 20, 20))
     to_pil(automatic_color_equalization(from_pil(self.img)))
def crop(filename, input_dir, output_dir, size=256, colorcorrect=False):
    filename = Path(filename)
    name = filename.name.replace(filename.suffix, '')

    path_in = Path(input_dir) / filename
    path_out_rgb_dir = Path(output_dir) / 'RGB'
    path_out_nir_dir = Path(output_dir) / 'NIR'

    ds = gdal.Open(str(path_in))
    data_matrix = ds.ReadAsArray()

    rgb = np.transpose(data_matrix[:3, :, :], (1, 2, 0))
    nir = data_matrix[3, :, :]

    height, width = nir.shape
    hnum = height // size
    wnum = width // size

    num = 0
    rejected = 0
    for h in range(hnum):
        for w in range(wnum):
            crop_rgb = rgb[size * h:size * h + size, size * w:size * w + size]
            crop_nir = nir[size * h:size * h + size, size * w:size * w + size]

            if not check_nodata(crop_rgb, crop_nir):
                rejected += 1
                continue

            if colorcorrect:
                crop_rgb = cca.stretch(cca.grey_world(crop_rgb))

            path_out_rgb = path_out_rgb_dir / '{}_{:06d}.png'.format(name, num)
            path_out_nir = path_out_nir_dir / '{}_{:06d}.png'.format(name, num)
            num += 1

            Image.fromarray(crop_rgb).save(path_out_rgb)
            Image.fromarray(crop_nir).save(path_out_nir)

    print('{} images rejected'.format(rejected))
    print('{} images generated'.format(num))
Esempio n. 6
0
def extract_and_save_features(path_to_tiles, path_to_save_features):
    """Extract ResNet features from tile images.
    """
    model = ResNet50(weights='imagenet', include_top=True)
    model = Model(inputs=model.inputs,
                  outputs=model.get_layer('avg_pool').output)

    if not os.path.exists(path_to_save_features):
        os.mkdir(path_to_save_features)
    for cat in [
            'ADI', 'MUC', 'BACK', 'LYM', 'NORM', 'DEB', 'MUS', 'STR', 'TUM'
    ]:
        X = []
        for filename in tqdm(os.listdir(os.path.join(path_to_tiles, cat))):
            try:
                tile = Image.open(os.path.join(path_to_tiles, cat, filename))
                tile = to_pil(cca.stretch(from_pil(tile)))
                tile = np.array(tile)
                features = model.predict(preprocess_input(tile[np.newaxis]),
                                         batch_size=1)
                X.append(features)
            except ZeroDivisionError:
                pass
        np.save(os.path.join(path_to_save_features, f'{cat}.npy'), np.array(X))
Esempio n. 7
0
import colorcorrect.algorithm as cca
import numpy as np
import sys


def from_pil(pimg):
    pimg = pimg.convert(mode='RGB')
    nimg = np.array(pimg)[:]
    # nimg.flags.writeable = True
    return nimg


def to_pil(nimg):
    return Image.fromarray(np.uint8(nimg))


if __name__ == "__main__":
    img = Image.open(sys.argv[1])
    # img.show()
    to_pil(cca.stretch(from_pil(img)))
    to_pil(cca.grey_world(from_pil(img)))
    to_pil(cca.retinex(from_pil(img)))
    to_pil(cca.max_white(from_pil(img)))
    to_pil(cca.retinex_with_adjust(cca.retinex(from_pil(img))))
    to_pil(cca.standard_deviation_weighted_grey_world(from_pil(img), 20, 20))
    to_pil(
        cca.standard_deviation_and_luminance_weighted_gray_world(
            from_pil(img), 20, 20))
    to_pil(cca.luminance_weighted_gray_world(from_pil(img), 20, 20))
    to_pil(cca.automatic_color_equalization(from_pil(img)))
Esempio n. 8
0
def find_new_KLT(cap, frame_index):

    face_found = False
    nose_mask = []
    old_gray = []
    p0_nose = []

    while(not face_found):

        ret,frame = cap.read()
        if ret == True:

            frame = cca.stretch(cv2.resize(frame, FRAME_RESIZE))
            
            cv2.imshow('frame',frame)
            frame_index += 1
            out.write(frame)
            cv2.waitKey(1)
            
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            (faces, eyes, noses) = get_features(gray)
            nose_mask = np.zeros(gray.shape)

            for (x,y,w,h) in faces:
            
                # Detect the face and save to DF
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0), 2)

                for (ex,ey,ew,eh) in eyes:
                    cv2.rectangle(frame,(x+ex,y+ey),(x+ex+ew,y+ey+eh),(0,255,0),2)

                print "Found face " + str(len(eyes)) + " " + str(len(noses))

                for (nx,ny,nw,nh) in noses:
                    cv2.rectangle(frame,(x+nx,y+ny),(x+nx+nw,y+ny+nh),(0,0,255),2)

                if (len(noses) == 1):

                    face_found = True
                    old_frame = frame

                    """
                    for (ex,ey,ew,eh) in eyes:
                        
                        # ex and ey are relative to the face frame, need to shift to entire frame
                        tx = ex+x
                        ty = ey+y
                        eye_mask[ty:ty+eh, tx:tx+ew] = 1.
                    """

                    for (nx,ny,nw,nh) in noses:

                        # ex and ey are relative to the face frame, need to shift to entire frame
                        tx = nx+x
                        ty = ny+y+h/3
                        nose_mask[ty:ty+nh, tx:tx+nw] = 1.

                    nose_mask = nose_mask.astype(np.uint8)
                    break

    # Take first frame and find corners in it
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
    #p0_eyes = cv2.goodFeaturesToTrack(old_gray, mask = eye_mask, **feature_params)
    p0_nose = cv2.goodFeaturesToTrack(old_gray, mask = nose_mask, **feature_params)

    return (old_gray, frame_index, frame, p0_nose)
Esempio n. 9
0
def getOneEvent(cap, frame_index, old_gray, p0_nose):

    ERROR_ALLOWANCE = 5

    if (cap.isOpened()):
        
        ret, frame = cap.read()

        if (ret == False):
            print "NO FRAME FOUND"
            raise NoFramesLeftError()

        else:

            # Within each iteration, there are 3 outcomes:
            # 1. We have KLT points which are stable and within the Viola-Area
            # 2. We have no KLT points, but we have a face. Initialize KLT.

            frame = cca.stretch(cv2.resize(frame, FRAME_RESIZE))
            frame_index += 1
            out.write(frame)
            cv2.waitKey(1)

            this_event = dict(
                time=time.time(),
                isFrontFace=0,
                faceLeft=-1,
                faceRight=-1,
                faceTop=-1,
                faceBottom=-1,
                noseX=-1,
                noseY=-1,
                frameIndex=-1
                )

            ### KLT : Optical Flow
            # calculate optical flow

            while (p0_nose == None or len(p0_nose) < 3):               
                (old_gray, index, frame, p0_nose) = find_new_KLT(cap, frame_index)
                frame_index = index

            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            p1_nose, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0_nose, None, **lk_params)

            good_new_nose = p1_nose[st==1]
            good_old_nose = p0_nose[st==1]

            # draw the tracks
            for i,(new,old) in enumerate(zip(good_new_nose,good_old_nose)):
                a,b = new.ravel()
                c,d = old.ravel()
                cv2.line(frame, (a,b),(c,d),(0,255,255), 2)
                cv2.circle(frame,(a,b),5,(0,255,255),-1)
                 
            # Now update the previous frame and previous points
            old_gray = frame_gray.copy()        
            p0_nose = good_new_nose.reshape(-1,1,2)

            this_event.update(dict(
                noseX=np.mean(p0_nose, axis=0)[0][0],
                noseY=np.mean(p0_nose, axis=0)[0][1],
                frameIndex = frame_index
                ))

            ################################################################################

            ### Viola-Jones : Regional Detection
            (faces, eyes, noses) = get_features(frame_gray)
                    
            if (len(faces) == 1):

                (x,y,w,h) = faces[0]
                    
                this_event.update(dict(
                    isFrontFace=1,
                    faceLeft=x,
                    faceRight=(x+w),
                    faceTop=(y),
                    faceBottom=(y+h)
                    ))

                # Detect the face and save to DF
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0), 2)

                for (ex,ey,ew,eh) in eyes:
                    cv2.rectangle(frame,(x+ex,y+ey),(x+ex+ew,y+ey+eh),(0,255,0),2)

                if (len(noses) == 1):
                    
                    (nx,ny,nw,nh) = noses[0]

                    i = 0
                    p0_nose_filter = np.ones(len(p0_nose), dtype=bool)

                    while i < len(p0_nose):    
                        px = p0_nose[i][0][0]
                        py = p0_nose[i][0][1]
                        
                        if not(px > x+nx-ERROR_ALLOWANCE and px < x+nx+nw+ERROR_ALLOWANCE) \
                            or not(py > y+ny+(h/3)-ERROR_ALLOWANCE and py < y+ny+nh+(h/3)+ERROR_ALLOWANCE):
                            p0_nose_filter[i] = False

                        i += 1

                    p0_nose = p0_nose[p0_nose_filter]
                    cv2.rectangle(frame,(x+nx,y+ny+(h/3)),(x+nx+nw,y+ny+nh+(h/3)),(0,0,255),2)

        cv2.imshow('frame',frame)
        cv2.waitKey(1)

        return (this_event, frame_index, old_gray, p0_nose)

    print "cap is closed"
    return
Esempio n. 10
0
def extract_tile_features(level, coord, zoom):
    tile = np.array(zoom.get_tile(level, (coord[1], coord[2])))
    tile = Image.fromarray(tile)
    tile = to_pil(cca.stretch(from_pil(tile)))
    tile = np.array(tile)
    return tile
# -*- coding: utf-8 -*-
import sys
from PIL import Image
from colorcorrect.algorithm import stretch, grey_world, retinex, retinex_with_adjust, max_white
from colorcorrect.algorithm import standard_deviation_weighted_grey_world
from colorcorrect.algorithm import standard_deviation_and_luminance_weighted_gray_world
from colorcorrect.algorithm import automatic_color_equalization
from colorcorrect.algorithm import luminance_weighted_gray_world
from colorcorrect.util import from_pil, to_pil

if __name__ == "__main__":
    img = Image.open(sys.argv[1])
    # img.show()
    to_pil(stretch(from_pil(img)))
    to_pil(grey_world(from_pil(img)))
    to_pil(retinex(from_pil(img)))
    to_pil(max_white(from_pil(img)))
    to_pil(retinex_with_adjust(retinex(from_pil(img))))
    to_pil(standard_deviation_weighted_grey_world(from_pil(img), 20, 20))
    to_pil(
        standard_deviation_and_luminance_weighted_gray_world(
            from_pil(img), 20, 20))
    to_pil(luminance_weighted_gray_world(from_pil(img), 20, 20))
    to_pil(automatic_color_equalization(from_pil(img)))
Esempio n. 12
0
       print("Creation of the directory %s failed" % colcor_path)
   else:
       print("Successfully created the directory %s " % colcor_path)

Iter = 0

print("Executing...")


for thisFile in os.listdir(target_dir):
    file_name = os.path.join(target_dir, thisFile)
    if os.path.isfile(file_name):
        file_name = os.path.join(target_dir, thisFile)

        Iter += 1
        print("\r" + str(Iter) + '/' + str(file_count), end='')

        img = Image.open(file_name)

        img = to_pil(cca.stretch(cca.gray_world(from_pil(img))))

        img = ImageOps.autocontrast(img, 0)
        img = img.filter(ImageFilter.SHARPEN)

        basename = os.path.basename(file_name)
        abs_filename = colcor_path +  '/' + basename
        img.save(abs_filename, quality=95)

print("\nDone.")

Esempio n. 13
0
def find_new_KLT(cap, frame_index, ideal_width, ideal_height):

    face_found = False
    nose_mask = []
    old_gray = []
    p0_nose = []

    while(not face_found):

        ret,frame = cap.read()
        if ret == True:

            frame = cca.stretch(cv2.resize(frame, FRAME_RESIZE))
            
            cv2.imshow('frame',frame)
            frame_index += 1
            out.write(frame)
            cv2.waitKey(1)
            
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            (faces, eyes, noses) = get_features(gray)
            nose_mask = np.zeros(gray.shape)

            # Determine best face
            
            # Multiple faces in the frame
            if len(faces) > 1:
            
                largest_w = -1

                #find the largest face
                for (x,y,w,h) in faces:
                    
                    if (w > largest_w):
                        # Face variables
                        fx = x
                        fy = y
                        fw = w
                        fh = h

            # Only one, just use it
            elif len(faces) == 1:
                    fx = faces[0][0]
                    fy = faces[0][1]
                    fw = faces[0][2]
                    fh = faces[0][3]

            else:
                continue

            # Detect the face and save to DF
            cv2.rectangle(frame,(fx,fy),(fx+fw,fy+fh),(255,0,0), 2)

            for (ex,ey,ew,eh) in eyes:
                cv2.rectangle(frame,(fx+ex,fy+ey),(fx+ex+ew,fy+ey+eh),(0,255,0),2)

            print "Found face " + str(len(eyes)) + " " + str(len(noses))

            for (nx,ny,nw,nh) in noses:
                cv2.rectangle(frame,(fx+nx,fy+ny),(fx+nx+nw,fy+ny+nh),(0,0,255),2)

            if (len(noses) == 1):

                # Not set yet: take first face as the ideal
                if (ideal_width < 0):
                    ideal_width = fw
                    ideal_height = fh
                    print "ideal w:" + str(ideal_width) + " h: " + str(ideal_height)

                else:
                    
                    if (fw < (0.9 * ideal_width) and fh < (0.9 * ideal_height)):
                        print "rejected due to size"

                    else:
                        face_found = True
                        old_frame = frame

                        for (nx,ny,nw,nh) in noses:

                            # ex and ey are relative to the face frame, need to shift to entire frame
                            tx = nx+fx
                            ty = ny+fy+fh/3
                            nose_mask[ty:ty+nh, tx:tx+nw] = 1.

                        nose_mask = nose_mask.astype(np.uint8)
                    

    # Take first frame and find corners in it
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
    #p0_eyes = cv2.goodFeaturesToTrack(old_gray, mask = eye_mask, **feature_params)
    p0_nose = cv2.goodFeaturesToTrack(old_gray, mask = nose_mask, **feature_params)

    return (old_gray, frame_index, frame, p0_nose, ideal_width, ideal_height)