def denoising_2_demo():
    """
    第一个参数是一个噪声帧的列表。
    第二个参数 imgtoDenoiseIndex 设定那些帧需要去噪,我们可以传入一 个帧的索引。
    第三个参数 temporaWindowSize 可以设置用于去噪的相邻帧的数目,它应该是一个奇数。
    在这种情况下 temporaWindowSize 帧的 图像会被用于去噪,中间的帧就是要去噪的帧。
    例如,我们传入 5 帧图像, imgToDenoiseIndex = 2 和 temporalWindowSize = 3。
    那么第一帧,第二帧, 第三帧图像将被用于第二帧图像的去噪

    """
    capture = cv.VideoCapture("../images/vtest.avi")
    image = [capture.read()[1] for i in range(5)]
    gray = [cv.cvtColor(i, cv.COLOR_BGR2GRAY) for i in image]
    gray = [np.float64(i) for i in gray]

    noise = np.random.randn(*gray[1].shape) * 10
    noisy = [i + noise for i in gray]
    noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]

    # fastNlMeansDenoisingColoredMulti(srcImgs, imgToDenoiseIndex, temporalWindowSize,
    # dst=None, h=None, hColor=None, templateWindowSize=None, searchWindowSize=None):
    dst = cv.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)
    plt.subplot(131), plt.imshow(gray[2], 'gray')
    plt.subplot(132), plt.imshow(noisy[2], 'gray')
    plt.subplot(133), plt.imshow(dst, 'gray')
    plt.show()
    def multi_denoising(self, img):
        # Enhance image
        img = cv2.fastNlMeansDenoisingMulti(img, imgToDenoiseIndex=2, temporalWindowSize=5, dst=None, h=1,
                                            templateWindowSize=7, searchWindowSize=21)

        cv2.imshow('image', img)
        cv2.waitKey(0)

        return img
Example #3
0
def reduceNoise(images, greyScale):
    if (len(images) > 1):
        if greyScale == 1:
            return cv.fastNlMeansDenoisingMulti(images, 0, len(images))
        else:
            return cv.fastNlMeansDenoisingColoredMulti(images, 1, len(images))
    elif (len(images) == 1):
        if greyScale == 1:
            return cv.fastNlMeansDenoising(images[0], None, 10)
Example #4
0
def fase2():
    from Arduino.Arduino import Arduino
    arduino = Arduino(baudrate=9600,timeout=1)
    ret = arduino.setup(port= 0)
    arduino.write("Is90F")
    arduino.write("Im0&0F")

    cap = cv2.VideoCapture(0)
    #Factor para pixelar la imagen manteninendo la raelación de aspecto
    factor_reescalado = 4
    #Respecto al denoise multiple almacena frames anteriores
    pila_denoise = []
    len_pila_denoise = 3
    while True:
        margin = 30
        in_front = 100
        ret,frame = cap.read()
        resized = cv2.resize(frame,(frame.shape[1]//factor_reescalado,frame.shape[0]//factor_reescalado))
        resized = cv2.resize(resized,(resized.shape[1]*factor_reescalado,resized.shape[0]*factor_reescalado))
        #Denoise multiple
        dst = resized
        pila_denoise.append(resized)
        if(len(pila_denoise)>len_pila_denoise):
            #https://docs.opencv.org/3.4/d1/d79/group__photo__denoise.html#ga723ffde1969430fede9241402e198151
            dst = cv2.fastNlMeansDenoisingMulti(pila_denoise, len(pila_denoise)-1, (len_pila_denoise-1)//2, None, 4, 7, 5)
            del pila_denoise[0]

        blur = cv2.GaussianBlur(dst,(5,5),0)
        gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
        edges = auto_canny(gray,sigma = 0.7)
        circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,35,
            param1=50,param2=30,minRadius=0,maxRadius=edges.shape[1])

        if (circles is not None):
            circles = np.round(circles[0,:].astype("int"))
            for i in circles:
                #cv2.circle(gray,(i[0],i[1]),i[2],(0,255,0),2)
                #cv2.circle(gray,(i[0],i[1]),2,(0,0,255),3)
                while(check_size()<in_front):
                    if(i[1]>frame.shape[1]/2+margin):
                        arduino.move_motors(240,0)
                    elif(i[1]<frame.shape[1]/2-margin):
                        arduino.move_motors(0,240)
                    else:
                        arduino.move_motors(240,240)
                    sleep(0.01)
                recoger()

        res = np.hstack((gray,edges))
        #cv2.imshow("resized",res)
        cv2.imshow("dst",res)
        k = cv2.waitKey(1)
        if (ord('k')==k):
            cv2.destroyAllWindows()
            break
    pass
    def denoising(self, images):
        # todo: need to be optimized later
        # todo: can be extended to multiple images, like cv.fastNlMeansDenoisingColoredMulti
        # todo: Or to run a super-resolution model,
        # need to maintain another internal list/dictionary
        # denoised_image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
        denoised_image = cv2.fastNlMeansDenoisingMulti(images, 2, 5, None, 4,
                                                       7, 35)

        return denoised_image
def denoise_depth(depth_images: list, temporal_window_size=7) -> np.ndarray:
    return cv2.fastNlMeansDenoisingMulti(
        depth_images,
        imgToDenoiseIndex=3,
        temporalWindowSize=temporal_window_size,
        dst=None,
        templateWindowSize=7,
        searchWindowSize=21,
        # normType=NORM_L1
    )
def denoising_video_grayscale(itdi, sq, vid, fs, tps, ws, tws):
    for i in range(itdi):
        sq.append(cv2.fastNlMeansDenoising(vid[i], None, fs, tps, ws))

    for k in range(len(vid) - itdi * 2):
        l_tws = [vid[k + 1] for i in range(tws)]
        sq.append(
            cv2.fastNlMeansDenoisingMulti(l_tws, itdi, tws, None, fs, tps, ws))

    for i in range(itdi):
        sq.append(
            cv2.fastNlMeansDenoising(vid[len(vid) - itdi + i], None, fs, tps,
                                     ws))
Example #8
0
def apply_denoising(img):
	# create a list of first 5 frames
	#img = [cap.read()[1] for i in xrange(5)]
	# convert all to grayscale
	gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]
	# convert all to float64
	#gray = [np.float64(i) for i in gray]
	# create a noise of variance 25
	#noise = np.random.randn(*gray[1].shape)*10
	# Add this noise to images
	#noisy = [i+noise for i in gray]
	# Convert back to uint8
	#noisy = [np.uint8(np.clip(i,0,255)) for i in noisy]
	# Denoise 3rd frame considering all the 5 frames
	dst = cv2.fastNlMeansDenoisingMulti(gray, 1, 3, None, 4, 7, 35)
	return dst
 def denoise_video(self):
     cap = cv2.VideoCapture('../images/vtest.avi')
     img = [cap.read()[1] for i in range(5)]# 创建一个前5帧的list
     gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]# 全部转为灰度图
     gray = [np.float64(i) for i in gray]# 全部转为float64类型
     noise = np.random.randn(*gray[1].shape) * 10#创建一个方差为25的噪声
     noisy = [i + noise for i in gray]#向所有图像添加噪声
     noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]#转为uint8类型
     # 根据这5帧图像,对第三帧去噪
     dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)
     titles = ['3rd frame', '3rd frame+noise','3rd denoise']
     imgs = [gray[2], noisy[2],dst]
     for i in range(3):
         plt.subplot(1, 3, i + 1), plt.imshow(imgs[i], 'gray')
         plt.title(titles[i])
         plt.xticks([]), plt.yticks([])
     plt.show()
Example #10
0
def fase2():
    cap = cv2.VideoCapture(0)
    #Factor para pixelar la imagen manteninendo la raelación de aspecto
    factor_reescalado = 4
    #Respecto al denoise multiple almacena frames anteriores
    pila_denoise = []
    len_pila_denoise = 3
    while True:
        ret,frame = cap.read()
        resized = cv2.resize(frame,(frame.shape[1]//factor_reescalado,frame.shape[0]//factor_reescalado))
        resized = cv2.resize(resized,(resized.shape[1]*factor_reescalado,resized.shape[0]*factor_reescalado))
        #Denoise multiple
        dst = resized

        pila_denoise.append(resized)
        if(len(pila_denoise)>len_pila_denoise):
            #https://docs.opencv.org/3.4/d1/d79/group__photo__denoise.html#ga723ffde1969430fede9241402e198151
            dst = cv2.fastNlMeansDenoisingMulti(pila_denoise, len(pila_denoise)-1, (len_pila_denoise-1)//2, None, 4, 7, 5)
            del pila_denoise[0]

        blur = cv2.GaussianBlur(dst,(5,5),0)
        gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

        edges = auto_canny(gray,sigma = 0.7)

        ###################
        circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,35,
            param1=50,param2=30,minRadius=0,maxRadius=edges.shape[1])

        if (circles is not None):
            circles = np.round(circles[0,:].astype("int"))
            for i in circles:
                # draw the outer circle
                cv2.circle(gray,(i[0],i[1]),i[2],(0,255,0),2)
                # draw the center of the circle
                cv2.circle(gray,(i[0],i[1]),2,(0,0,255),3)

        res = np.hstack((gray,edges))
        #cv2.imshow("resized",res)
        cv2.imshow("dst",res)
        k = cv2.waitKey(1)
        if (ord('k')==k):
            cv2.destroyAllWindows()
            break
    pass
Example #11
0
def denoise(source: str, destination: str) -> None:
    vid = _cv.VideoCapture(source)

    # (t,y,x,chan)
    color_movie = _get_frames(vid)

    temporal_win_size = _cfg.denoise.temporal_window_size
    strength = _cfg.denoise.strength
    template_win_size = _cfg.denoise.template_window_size
    search_win_size = _cfg.denoise.search_window_size

    print("Denoising...")
    # (t,y,x,chan)
    denoised_color_movie = _np.zeros(color_movie.shape,
                                     color_movie.dtype)  # type: ignore
    for index in range(color_movie.shape[0]):
        print(f"  {index + 1: 5}/{color_movie.shape[0]}\u000D", end="")
        if (index < temporal_win_size // 2 or
            (color_movie.shape[0] - 1) - index < temporal_win_size // 2):
            denoised_color_movie[index] = color_movie[index]
            continue
        denoised_color_movie[index] = _cv.fastNlMeansDenoisingMulti(
            color_movie,
            index,
            temporal_win_size,
            h=strength,
            templateWindowSize=template_win_size,
            searchWindowSize=search_win_size,
        )
    else:
        print("")

    # Save results
    _matrix_to_video(
        denoised_color_movie,
        destination,
        "mp4v",
        vid.get(_cv.CAP_PROP_FPS),
        int(vid.get(_cv.CAP_PROP_FRAME_WIDTH)),
        int(vid.get(_cv.CAP_PROP_FRAME_HEIGHT)),
    )

    vid.release()
Example #12
0
 def test_4912(self):
     cap = cv2.VideoCapture('vtest.avi')
     # create a list of first 5 frames
     img = [cap.read()[1] for i in range(5)]
     # convert all to grayscale
     gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]
     # convert all to float64
     gray = [np.float64(i) for i in gray]
     # create a noise of variance 25
     noise = np.random.randn(*gray[1].shape) * 10
     # Add this noise to images
     noisy = [i + noise for i in gray]
     # Convert back to uint8
     noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]
     # Denoise 3rd frame considering all the 5 frames
     dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)
     plt.subplot(131), plt.imshow(gray[2], 'gray')
     plt.subplot(132), plt.imshow(noisy[2], 'gray')
     plt.subplot(133), plt.imshow(dst, 'gray')
     plt.show()
     print("")
Example #13
0
import numpy as np
import cv2
from matplotlib import pyplot as plt

cap = cv2.VideoCapture('multi_ants.mov')

# create a list of first 5 frames
img = [cap.read()[1] for i in range(5)]

# convert all to grayscale
gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]

# convert all to float64
gray = [np.float64(i) for i in gray]

# create a noise of variance 25
noise = np.random.randn(*gray[1].shape) * 10

# Add this noise to images
noisy = [i + noise for i in gray]

# Convert back to uint8
noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]

# Denoise 3rd frame considering all the 5 frames
dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)

while (1):
    ret
Example #14
0
def main():
    allContoursWithData = []                # declare empty lists,
    validContoursWithData = []              # we will fill these shortly

    try:
        npaClassifications = np.loadtxt("classifications.txt", np.float32)                  # read in training classifications
#        print "lOADED CLASSIFICATIONS"
    except:
        print "error, unable to open classifications.txt, exiting program\n"
        os.system("pause")
        return
    # end try

    try:
        npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32)                 # read in training images
    except:
        print "error, unable to open flattened_images.txt, exiting program\n"
        os.system("pause")
        return
    # end try

    npaClassifications = npaClassifications.reshape((npaClassifications.size, 1))       # reshape numpy array to 1d, necessary to pass to call to train

    kNearest = cv2.ml.KNearest_create()                   # instantiate KNN object

    kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

    imgTestingNumbers = cv2.imread(sys.argv[1])          # read in testing numbers image

    if imgTestingNumbers is None:                           # if image was not read successfully
        print "error: image not read from file \n\n"        # print error message to std out
        os.system("pause")                                  # pause so user can see error message
        return                                              # and exit function (which exits program)
    # end if


    imgGray = cv2.cvtColor(imgTestingNumbers, cv2.COLOR_BGR2GRAY)       # get grayscale image
#    cv2.imshow("imgGray", imgGray)
    imgBlurred = cv2.GaussianBlur(imgGray, (5,5), 0)                    # blur
    #cv2.imshow("imgBlurred", imgBlurred)
                                                        # filter image from grayscale to black and white
    imgThresh = cv2.adaptiveThreshold(imgBlurred,                           # input image
                                      255,                                  # make pixels that pass the threshold full white
                                      cv2.ADAPTIVE_THRESH_GAUSSIAN_C,       # use gaussian rather than mean, seems to give better results
                                      cv2.THRESH_BINARY_INV,                # invert so foreground will be white, background will be black
                                      11,                                   # size of a pixel neighborhood used to calculate threshold value
                                      2)                                    # constant subtracted from the mean or weighted mean

    imgThreshCopy = imgThresh.copy()        # make a copy of the thresh image, this in necessary b/c findContours modifies the image

    noNoise = cv2.fastNlMeansDenoisingMulti(imgBlurred, 2, 5, None, 4, 7 , 35)

#    cv2.imshow("test", noNoise)

#    cv2.imshow("imgThreshCopy", imgThreshCopy)

    imgContours, npaContours, npaHierarchy = cv2.findContours(imgThreshCopy,             # input image, make sure to use a copy since the function will modify this image in the course of finding contours
                                                 cv2.RETR_EXTERNAL,         # retrieve the outermost contours only
                                                 cv2.CHAIN_APPROX_SIMPLE)   # compress horizontal, vertical, and diagonal segments and leave only their end points

    for npaContour in npaContours:                             # for each contour
        contourWithData = ContourWithData()                                             # instantiate a contour with data object
        contourWithData.npaContour = npaContour                                         # assign contour to contour with data
        contourWithData.boundingRect = cv2.boundingRect(contourWithData.npaContour)     # get the bounding rect
        contourWithData.calculateRectTopLeftPointAndWidthAndHeight()                    # get bounding rect info
        contourWithData.fltArea = cv2.contourArea(contourWithData.npaContour)           # calculate the contour area
        allContoursWithData.append(contourWithData)                                     # add contour with data object to list of all contours with data
    # end for

    for contourWithData in allContoursWithData:                 # for all contours
        if contourWithData.checkIfContourIsValid():             # check if valid
            validContoursWithData.append(contourWithData)       # if so, append to valid contour list
        # end if
    # end for

    validContoursWithData.sort(key = operator.attrgetter("intRectX"))         # sort contours from left to right

    strFinalString = ""         # declare final string, this will have the final number sequence by the end of the program

    for contourWithData in validContoursWithData:            # for each contour
                                                # draw a green rect around the current char
        cv2.rectangle(imgTestingNumbers,                                        # draw rectangle on original testing image
                      (contourWithData.intRectX, contourWithData.intRectY),     # upper left corner
                      (contourWithData.intRectX + contourWithData.intRectWidth, contourWithData.intRectY + contourWithData.intRectHeight),      # lower right corner
                      (0, 255, 0),              # green
                      2)                        # thickness

        imgROI = imgThresh[contourWithData.intRectY : contourWithData.intRectY + contourWithData.intRectHeight,     # crop char out of threshold image
                           contourWithData.intRectX : contourWithData.intRectX + contourWithData.intRectWidth]

        imgROIResized = cv2.resize(imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))             # resize image, this will be more consistent for recognition and storage

        npaROIResized = imgROIResized.reshape((1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))      # flatten image into 1d numpy array

        npaROIResized = np.float32(npaROIResized)       # convert from 1d numpy array of ints to 1d numpy array of floats

        retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 1)     # call KNN function find_nearest

        strCurrentChar = str(chr(int(npaResults[0][0])))                                             # get character from results

        strFinalString = strFinalString + strCurrentChar            # append current char to full string
    # end for

    print "\n" + strFinalString + "\n"                  # show the full string
    file = open(sys.argv[2],"w")
    file.write(strFinalString)
    file.close()

	#    cv2.imshow("imgTestingNumbers", imgTestingNumbers)      # show input image with green boxes drawn around found digits
    cv2.waitKey(0)                                          # wait for user key press

    cv2.destroyAllWindows()             # remove windows from memory

    return
def _video_smoothing(le_data_dir,
                     div,
                     save_dir,
                     raw_video_fpath,
                     smoothing_method="avg_diff"):
    fname = osp.splitext(osp.basename(raw_video_fpath))[0]
    label = raw_video_fpath.split(os.sep)[-2]

    raw_frame_list = read_video(raw_video_fpath)
    le_frame_list = read_video(
        osp.join(le_data_dir, div, label, f"{fname}.avi"))

    if smoothing_method == "avg_diff":
        weight = [1 / 5, 1 / 5, 1 / 5, 1 / 5, 1 / 5]
        # get difference frame between raw and le
        diff_frame_list = []
        for raw, le in zip(raw_frame_list, le_frame_list):
            raw = raw.astype(np.float32)
            le = le.astype(np.float32)

            diff_frame = raw - le
            diff_frame_list.append(diff_frame)

        # get smoothed le
        smooth_frame_list = []
        for i, raw in enumerate(raw_frame_list):
            smooth = []
            for j, w in enumerate(weight):
                # handling overflow
                idx = i + j - int(len(weight) / 2)
                idx = np.clip(idx, 0, len(diff_frame_list) - 1)
                smooth.append(w * diff_frame_list[idx])
            smooth = np.stack(smooth)
            smooth = np.sum(smooth, axis=0)

            le = raw.astype(np.float32) - smooth
            le = np.clip(le, 0, 255)
            le = le.astype(np.uint8)

            smooth_frame_list.append(le)

    elif smoothing_method == "denoising":
        num_frame = 5

        smooth_frame_list = []
        for i, raw in enumerate(le_frame_list):
            smooth = []
            for j in range(num_frame):
                # handling overflow
                idx = i + j - int(num_frame / 2)
                idx = np.clip(idx, 0, len(le_frame_list) - 1)
                smooth.append(le_frame_list[idx])

            smooth = cv2.fastNlMeansDenoisingMulti(smooth, 2, 5, None, 4, 7,
                                                   35)
            smooth_frame_list.append(smooth)

    # save results
    if not osp.isdir(osp.join(save_dir, div, label)):
        os.makedirs(osp.join(save_dir, div, label))
    save_video(smooth_frame_list, osp.join(save_dir, div, label,
                                           f"{fname}.avi"))
Example #16
0
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('hallsmooth17.mp4')
#think about setting the frame height and width before the processing
out = cv2.VideoWriter('output4.avi',cv2.cv.CV_FOURCC('M','J','P','G'), 20.0, (360,288))

while(True):
    ret, frame = cap.read()
    median = cv2.medianBlur(frame,5)
    dst = cv2.fastNlMeansDenoisingMulti(median, 2, 5, None, 4, 7, 35)
    cv2.imshow('frame',dst)
    out.write(dst)
    if cv2.waitKey(1) & 0xFF == ord('q'):
          break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Example #17
0
# create a list of first 5 frames
frame = [cap.read()[1] for i in range(5)]

# convert all to grayscale
gray = [cv.cvtColor(i, cv.COLOR_BGR2GRAY) for i in frame]

# convert all to float64
gray = [np.float64(i) for i in gray]

# create a noise of variance 25
noise = np.random.randn(*gray[1].shape) * 10

# Add this noise to images
noisy = [i + noise for i in gray]

# Convert back to uint8
noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]

# Denoise 3rd frame considering all the 5 frames
ret_frame = cv.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)

# show
images = [img, ret_img, gray[2], noisy[2], ret_frame]
colors = ['gray', 'gray', 'gray', 'gray', 'gray']
titles = ["img", "ret_img", "gray", "noisy", "ret_frame"]
for i in range(len(images)):
    plt.subplot(2, 3, i + 1)
    plt.imshow(images[i], colors[i])
    plt.title(titles[i])
plt.show()
Example #18
0
from matplotlib import pyplot as plt

cap = cv2.VideoCapture(0)
while (1):

    img = [cap.read()[1] for i in xrange(5)]

    gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]

    gray = [np.float64(i) for i in gray]

    noise = np.random.randn(*gray[1].shape) * 10

    noisy = [i + noise for i in gray]

    noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]

    dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)

    plt.subplot(131), plt.imshow(gray[2], 'gray')
    plt.subplot(132), plt.imshow(noisy[2], 'gray')
    plt.subplot(133), plt.imshow(dst, 'gray')
    plt.show()

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()
Example #19
0
import numpy as np
import cv2

a = []
a.append(cv2.imread("data/a1.jpg"))
a.append(cv2.imread("data/a2.jpg"))
a.append(cv2.imread("data/a3.jpg"))
a.append(cv2.imread("data/a4.jpg"))
a.append(cv2.imread("data/a5.jpg"))
a.append(cv2.imread("data/a6.jpg"))
a.append(cv2.imread("data/a7.jpg"))
a.append(cv2.imread("data/a8.jpg"))
a.append(cv2.imread("data/a9.jpg"))

gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in a]

# Convert back to uint8
gray = [np.uint8(np.clip(i,0,255)) for i in gray]

# Denoise 3rd frame considering all the 5 frames
dst = cv2.fastNlMeansDenoisingMulti(gray, 4, 9, None, 3, 7, 21)

cv2.imshow("noise reduction", dst)
cv2.waitKey(0)
Example #20
0
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('hallsmooth17.mp4')
#think about setting the frame height and width before the processing
out = cv2.VideoWriter('output4.avi', cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'),
                      20.0, (360, 288))

while (True):
    ret, frame = cap.read()
    median = cv2.medianBlur(frame, 5)
    dst = cv2.fastNlMeansDenoisingMulti(median, 2, 5, None, 4, 7, 35)
    cv2.imshow('frame', dst)
    out.write(dst)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Example #21
0
import cv2
import numpy as np
import time
'''
your computer will be destroyed
camera extracting beautiful out of series of image
'''
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
time.sleep(2)
while True:
    image_list = []
    for i in range(5):
        ret, frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = cv2.flip(frame, 180)
        image_list.append(frame)
        #name = chr(97 + i) + '.png'
        #cv2.imwrite(name,frame)
    dst = cv2.fastNlMeansDenoisingMulti(image_list, 2, 5, None, 4, 7, 35)
    cv2.imshow('destination', dst)
    #type q to get out
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    #cv2.imwrite('result.png',dst)
cap.release()
cv2.destroyAllWindows()
Example #22
0
kernel_5x5 = np.ones((5,5) , np.float32) / 25
kernel_7x7 = np.ones((7,7) , no.float32) / 49

#Blur - Convolotion
blurred = cv2.filter2D(image, -1, kernel_3x3)

#Blur - Averaged
blured_box = cv2.blur(image, (3,3))

#Blur - Gaussian
blurred_gauss = cv2.GaussianBlur(image, (7,7), 0)

#Blur Median
blurred_median = cv2.medianBlur(image, 5) #Painted type effect

#Blur - Bilateral
blurred_bileteral = cv2.bilateralFilter(image, 9, 75, 75) #Very effective noise but keeps edges sharp

#Image Denoising
#Nonlocal Means Denoising (Cleaning noise and making image more DSLR)
dst = cv2.fastNlMeansDenoisingColored(image, None, 6, 6, 7, 21) # For color image
      cv2.fastNlMeansDenoising()  # Works with single grayscale image
      cv2.fastNlMeansDenoisingMulti() # works with image sequence captured in short period of time (grayscale)
      cv2.fastNlMeansDenoisingColoredMulti() # works with image sequence captured in short period of time (color)


#Sharpening
|-1 -1 -1|
|-1 +9 -1| #Sum is 1 so don't need to normalization
|-1 -1 -1|
Example #23
0
 def denoise(imgs):
     return (cv.fastNlMeansDenoisingMulti(imgs, 2, 5, None, h,
                                          templateWindowSize,
                                          searchWindowPixel))
Example #24
0
cv2.waitKey(0)

"""
Python: cv2.fastNlMeansDenoisingMulti(srcImgs, imgToDenoiseIndex, temporalWindowSize[, dst[, h[, templateWindowSize[, searchWindowSize]]]]) -> dst
Parameters:	
srcImgs – Input 8-bit 1-channel, 2-channel or 3-channel images sequence. All images 
should have the same type and size.
imgToDenoiseIndex – Target image to denoise index in srcImgs sequence
temporalWindowSize – Number of surrounding images to use for target image denoising. 
Should be odd. 
Images from imgToDenoiseIndex - temporalWindowSize / 2 to imgToDenoiseIndex - temporalWindowSize / 2 
from srcImgs will be used to denoise srcImgs[imgToDenoiseIndex] image.
dst – Output image with the same size and type as srcImgs images.
templateWindowSize – Size in pixels of the template patch that is used to compute weights. 
Should be odd. Recommended value 7 pixels
searchWindowSize – Size in pixels of the window that is used to compute weighted average 
for given pixel. Should be odd. Affect performance linearly: 
    greater searchWindowsSize - greater denoising time. Recommended value 21 pixels
h – Parameter regulating filter strength for luminance component. Bigger h value perfectly 
removes noise but also removes image details, smaller h value preserves details but also 
preserves some noise
"""
dst2 = cv2.fastNlMeansDenoisingMulti([gray], 0, 1, None, 6, 7, 21)
cv2.imshow('Fast Means Denoising Multi', dst2)
cv2.waitKey(0)

dst3 = cv2.fastNlMeansDenoisingColoredMulti([image], 0, 1, None, 6, 6, 7, 21)
cv2.imshow('Fast Means Denoising Multi (color)', dst3)
cv2.waitKey(0)

cv2.destroyAllWindows()
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('/home/qiao/PythonProjects/Opencv_On_CT/Test_Img/10.jpg', 0)
dst = cv.fastNlMeansDenoisingMulti(img, 2, 5, None, 4, 7, 35)

plt.subplot(121), plt.imshow(img)
plt.subplot(122), plt.imshow(dst)
plt.show()
Example #26
0
彩色图像算法

fastNlMeansDenoisingColored(src[, dst[, h[, hColor[, templateWindowSize[, searchWindowSize]]]]]) -> dst
h=3, hColor=3, tempWindow=7, searchWindow=21

推荐参数
噪声 ∂        块大小 s  搜索窗口     衰减参数 h
0< ∂ =<25   |3 x 3  |21 x 21    |0.55 * ∂   |
25< ∂ =<55  |5 x 5  |35 x 35    |0.40 * ∂   |
55< ∂ =<100 |7 x 7  |35 x 35    |0.35 * ∂   |

"""
color = cv2.fastNlMeansDenoisingColored(img)
"""
适用于顺序帧序列的去噪声方法

1.
fastNlMeansDenoisingMulti(srcImgs, imgToDenoiseIndex, temporalWindowSize[, dst[, h[, templateWindowSize[, searchWindowSize]]]]) -> dst

2.
fastNlMeansDenoisingColoredMulti(srcImgs, imgToDenoiseIndex, temporalWindowSize[, dst[, h[, hColor[, templateWindowSize[, searchWindowSize]]]]]) -> dst

"""
denoising = cv2.fastNlMeansDenoisingMulti()
denoising = cv2.fastNlMeansDenoisingColoredMulti()
"""
直方图均衡化处理

需要注意的是源图必须是 8bit一维图像,所以彩色图像必须分通道处理
"""
Hist = cv2.equalizeHist(img)
Example #27
0
def filter_block(info, inputs, outputs, otherargs):

    # print('Current block is')
    # print(info.getBlockCount())
    # print('out of')
    # print(info.getTotalBlocks())

    if not otherargs.filter in ['NLM', 'median', 'mean']:
        raise SyntaxError(otherargs.filter +
                          ' is not a valid filter. Use NLM, median, or mean')
    if (otherargs.window_size % 2) == 0:
        raise SyntaxError('window size must be an odd integer')
    if not 0 <= otherargs.cc_thresh < 1:
        raise SyntaxError('correlation threshold must be  >=0 and <1')
    if otherargs.mask:
        if len(inputs.offsets) == inputs.cc:
            raise SyntaxError(
                'if mask=True the length of the input lists must be identical')
    if otherargs.filter == 'NLM':
        nlm_h = otherargs.nlm_h
        nlm_search_size = otherargs.nlm_search_size

    filter = otherargs.filter
    window_size = otherargs.window_size
    px_block = inputs.offsets
    forward_list = otherargs.forward_list

    # DEBUG
    # from rios.imagereader import ImageReader
    # from rios.readerinfo import ReaderInfo
    # reader = ImageReader(inputs.offsets, overlap=otherargs.window_size,  windowxsize=block_size, windowysize=block_size)
    # px_block = reader.readBlock(0)
    # block_coordinates_x, block_coordinates_y = px_block[0].getBlockCoordArrays()
    # px_block = px_block[1]
    # print(block_coordinates_x[0,0])
    # print(block_coordinates_y[0,0])

    # masking if requested
    if otherargs.mask:
        cc_block = inputs.cc

        # DEBUG
        # reader = ImageReader(inputs.cc, overlap=otherargs.window_size, windowxsize=block_size, windowysize=block_size)
        # cc_block = reader.readBlock(0)
        # cc_block = cc_block[1]

        # print('This is cc_block')
        # print(cc_block)
        # print(len(cc_block))
        cc_thresh_real = otherargs.cc_thresh * 128 + 127
        cc_bool = []
        for cc_patch in cc_block:
            # print('Shape of cc_patch is..')
            # print(cc_patch.shape)
            cc_patch = cc_patch[0]
            cc_bool.append(cc_patch < cc_thresh_real)

        # print('This is cc_bool is..')
        # print(cc_bool)
        # print(len(cc_bool))
        px_patch_masked = []
        for px_patch, cc_patch, forward in zip(px_block, cc_bool,
                                               forward_list):
            # print('Shape of px_patch is..')
            # print(px_patch.shape)
            px_patch = px_patch[0] * forward
            px_patch = np.ma.masked_array(px_patch, mask=cc_patch)
            px_patch_masked.append(
                np.ma.masked_where(
                    np.absolute(px_patch) > abs(otherargs.max_displacement),
                    px_patch))
    else:
        px_patch_masked = []
        for px_patch, forward in zip(px_block, forward_list):
            px_patch = px_patch[0] * forward
            px_patch_masked.append(px_patch)

        # plt.imshow(cc_patch)
        # plt.imshow(cc_bool[1])
        # plt.imshow(px_patch_masked[1])

    if filter == 'median' or filter == 'mean':

        px_patch_stack = np.ma.dstack(px_patch_masked)
        # print(px_patch_stack.dtype)
        if otherargs.mask:
            px_patch_stack = px_patch_stack.filled(np.nan)
        # print('Shape of px_patch_stack is..')
        # print(px_patch_stack.shape)
        w, h, d = px_patch_stack.shape
        # array_mask = image.mask
        strided_image = np.lib.stride_tricks.as_strided(
            px_patch_stack,
            shape=[
                w - window_size + 1, h - window_size + 1, 1, window_size,
                window_size, d
            ],
            strides=px_patch_stack.strides + px_patch_stack.strides)
        strided_image = np.squeeze(strided_image)

        # print('Shape of strided stack is..')
        # print(strided_image.shape)

        if filter == 'median':
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                average_image = np.nanmedian(strided_image, axis=(2, 3, 4))

        if filter == 'mean':
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                average_image = np.nanmean(strided_image, axis=(2, 3, 4))

        # padding to get shape expected by the rios writer
        average_image = np.lib.pad(average_image,
                                   int((window_size - 1) / 2),
                                   'constant',
                                   constant_values=0)
        # plt.imshow(average_image)

        average_image = np.expand_dims(average_image, axis=0)
        # print('Shape of average_image is..')
        # print(average_image.shape)

    if filter == 'NLM':

        # stretch input to 0,255 range required by the OpenCV implementation
        min_max = [
            func(l) for l in px_patch_masked for func in (np.min, np.max)
        ]
        min_px1 = np.min(min_max)
        max_px1 = np.max(min_max)
        noisy = [(px1_patch - min_px1) / (max_px1 - min_px1) * 255
                 for px1_patch in px_patch_masked]
        noisy = [np.uint8(i) for i in noisy]

        # fig, axs = plt.subplots(3,3)
        # for ax, image in zip(axs.reshape(-1), noisy):
        #     ax = ax.imshow(image, vmin=0, vmax=255)

        # Denoise with NLM
        target_img = int(len(noisy) / 2)
        temp_window = (len(noisy) - target_img) // 2 * 2 + 1

        average_image = cv2.fastNlMeansDenoisingMulti(noisy, target_img,
                                                      temp_window, None, nlm_h,
                                                      window_size,
                                                      nlm_search_size)
        # convert to pixel
        average_image = average_image / 255 * (max_px1 - min_px1) + min_px1

        # fig, axs = plt.subplots(1,2)
        # axs[0].imshow(average_image, vmin=-2, vmax=2)
        # axs[1].imshow(px_block[1][0][0, :, :], vmin=-2, vmax=2)

        # np.array_equal(px1_median_filtered[ :, :, 0], px1_median_filtered[ :, :, 1])
        # plt.imshow(px1_median_filtered[ :, :, 0] - px1_median_filtered[ :, :, 1])
        #
        #
        # fig, axs = plt.subplots(2,3)
        # axs[0,0].imshow(px1_median_filtered[ :, :, 0], vmin=-2, vmax=2)
        # axs[0,0].set_title("Median filter 21*21")
        # axs[0,1].imshow(px1_median_filtered[ :, :, 1], vmin=-2, vmax=2)
        # axs[0,1].set_title("Median filter 21*21")
        # axs[0,2].imshow(px1_median_filtered[ :, :, 6], vmin=-2, vmax=2)
        # axs[0,2].set_title("Median filter 21*21")
        # axs[1,0].imshow(px1_block[1][0][0, :, :], vmin=-2, vmax=2)
        # axs[1,0].set_title("Input")
        # axs[1,1].imshow(px1_denoised, vmin=-2, vmax=2)
        # axs[1,1].set_title("NLM_seq 3, 31, 51")
        # axs[1,2].imshow(px1_median_filtered_mean, vmin=-2, vmax=2)
        # axs[1,2].set_title("Median of the medians")

        # plt.close('all')

    if otherargs.numpy_outtype:
        average_image = average_image.astype(otherargs.numpy_outtype)

    outputs.outimage = average_image
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

cap = cv.VideoCapture("../img/vtest.avi")

# a list of first 5 frames
img = [cap.read()[1][:200, :200] for _ in range(5)]
grayed = [cv.cvtColor(i, cv.COLOR_BGR2GRAY) for i in img]
gray = [np.float64(i) for i in grayed]
noise = np.random.randn(
    *gray[1].shape) * 10  # gray[1].shape=(576, 768), *gray[1].shape=576, 768
noisy = [i + noise for i in gray]
noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]

# the second argument means which frame we need to denoise
# the third argument means how many nearby images we use to help denoise
dst = cv.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 6, 11, 35)

result = np.vstack((grayed[2], noisy[2], dst))
plt.imshow(result, cmap='gray')
plt.show()
plt.xticks([]), plt.yticks([])

cap.release()
Example #29
0
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (45, 45))
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (40, 40))

if camera.isOpened():
    grabbed, firstframe = camera.read()
else:
    grabbed = False

while grabbed:
    grabbed, frame = camera.read()
    frame = imutils.resize(frame, width=500)
    imgs = [camera.read()[1] for i in range(5)]
    imgs = [imutils.resize(img, width=500) for img in imgs]
    grays = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in imgs]
    fgmasks = [fgbg.apply(gray) for gray in grays]
    fgmask = cv2.fastNlMeansDenoisingMulti(fgmasks, 2, 5, None, 4, 7, 15)
    output2 = fgmask
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel2)
    output3 = fgmask
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel1)
    output4 = fgmask
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel2)
    fgmask = cv2.bilateralFilter(fgmask, 10, 75, 75)
    output5 = fgmask
    contours = cv2.findContours(fgmask, cv2.RETR_TREE,
                                cv2.CHAIN_APPROX_SIMPLE)[-2]
    rects = [cv2.boundingRect(cnt) for cnt in contours]
    for r in rects:
        x, y, w, h = r
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
Example #30
0
 def denoising(cls, img):
     img = cls.gray(img)
     return cv2.fastNlMeansDenoisingMulti(img, 2, 5, None, 4, 7, 35)
Example #31
0
def denoise1(imgs):
    img = cv2.fastNlMeansDenoisingMulti(imgs, 8, 1)
    return img
Example #32
0
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('hallsmooth17.mp4')
#think about setting the frame height and width before the processing
out = cv2.VideoWriter('output4.avi',cv2.cv.CV_FOURCC('M','J','P','G'), 20.0, (360,288))

while(True):
    ret, frame = cap.read()
    #blank_image=cv2.pyrDown(frame)
    #blank_image=cv2.bilateralFilter(frame,9,75,75)
    #gray_image = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
    dst = cv2.fastNlMeansDenoisingMulti(frame, 2, 5, None, 4, 7, 35)
    cv2.imshow('frame',frame)
    #out.write(blank_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
          break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Example #33
0
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('hallsmooth17.mp4')
#think about setting the frame height and width before the processing
out = cv2.VideoWriter('output4.avi', cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'),
                      20.0, (360, 288))

while (True):
    ret, frame = cap.read()
    #blank_image=cv2.pyrDown(frame)
    #blank_image=cv2.bilateralFilter(frame,9,75,75)
    #gray_image = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY)
    dst = cv2.fastNlMeansDenoisingMulti(frame, 2, 5, None, 4, 7, 35)
    cv2.imshow('frame', frame)
    #out.write(blank_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Example #34
0
def ImproveData(PathToFile, Pb, Pb2):
    cap = cv2.VideoCapture(PathToFile)
    videoName = os.path.basename(root.filename)
    videoName = videoName[:-4]
    img = []
    try:
        if not os.path.exists(videoName):
            os.makedirs(videoName + '/OriginalData')
            os.makedirs(videoName + '/DenoiseData')
            os.makedirs(videoName + '/ImageBordersData')
            os.makedirs(videoName + '/FinalData')
    except OSError:
        print('Error: Creating directory of data')
    currentFrame = 0
    T = bool(1)
    while (T):
        # Capture frame-by-frame
        ret, frame = cap.read()
        # Saves image of the current frame in png file
        name = './' + videoName + '/OriginalData/frame' + str(
            currentFrame) + '.png'
        # name = './OriginalData/frame' + str(currentFrame) + '.png'
        cv2.imwrite(name, frame)
        if os.path.getsize(name) == 0:
            T = bool(0)  # To stop duplicate images
            os.remove(name)
        else:
            currentFrame += 1
            img.append(frame)
    CountOfImages = currentFrame - 1
    # Denoising
    st = (1 / (CountOfImages - 4) * 100)
    for i in range(2, CountOfImages - 1):
        dst = cv2.fastNlMeansDenoisingMulti(img, i, 5, None, 4, 7, 35)
        #name = './DenoiseData/DenoisedFrame' + str(i) + '.png'
        name = './' + videoName + '/DenoiseData/DenoisedFrame' + str(
            i) + '.png'
        cv2.imwrite(name, dst)
        pb.step(st)
        pb.update()
    # Detection of borders in image
    Dic_of_Pixel = {}
    Original_Image = Image.open('./' + videoName +
                                '/DenoiseData/DenoisedFrame5.png')
    global width, height
    width, height = Original_Image.size
    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
    for k in range(2, CountOfImages - 1):
        name = './' + videoName + '/DenoiseData/DenoisedFrame' + str(
            k) + '.png'
        nameBorder = './' + videoName + '/ImageBordersData/BorderFrame' + str(
            k) + '.png'
        Grad(name, nameBorder, Dic_of_Pixel)
        nameFinal = './' + videoName + '/FinalData/FinalFrame' + str(
            k) + '.png'
        # Sharpen of image
        Sharpened(name, nameFinal, Dic_of_Pixel, kernel)
        Dic_of_Pixel.clear()
        pb2.step(st)
        pb2.update()

    # Creat video (in .mov and .mp4 format) from images in FinalData, DenoiseData, ImageBordersData
    image_folder = './' + videoName + '/FinalData/'
    video_name = './' + videoName + '/DenoisedVideoFinalData.mov'
    video_name2 = './' + videoName + '/DenoisedVideoFinalData.mp4'
    image_folderDen = './' + videoName + '/DenoiseData/'
    video_nameDen = './' + videoName + '/DenoisedVideoDenoisedData.mov'
    video_nameDen2 = './' + videoName + '/DenoisedVideoDenoisedData.mp4'
    image_folderBor = './' + videoName + '/ImageBordersData/'
    video_nameBor = './' + videoName + '/BordersVideo.mov'
    video_nameBor2 = './' + videoName + '/BordersVideo.mp4'

    images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
    imagesDen = [
        img for img in os.listdir(image_folderDen) if img.endswith(".png")
    ]
    imagesBor = [
        img for img in os.listdir(image_folderBor) if img.endswith(".png")
    ]

    imag = []
    imagDen = []
    imagBor = []

    for i in range(2, CountOfImages - 1):
        imag.append('FinalFrame' + str(i) + '.png')
        imagDen.append('DenoisedFrame' + str(i) + '.png')
        imagBor.append('BorderFrame' + str(i) + '.png')
    frame = cv2.imread(os.path.join(image_folder, images[0]))
    height, width, layers = frame.shape
    video = cv2.VideoWriter(video_name, -1, cap.get(cv2.CAP_PROP_FPS),
                            (width, height))
    video2 = cv2.VideoWriter(video_name2, -1, cap.get(cv2.CAP_PROP_FPS),
                             (width, height))
    video3 = cv2.VideoWriter(video_nameDen, -1, cap.get(cv2.CAP_PROP_FPS),
                             (width, height))
    video4 = cv2.VideoWriter(video_nameDen2, -1, cap.get(cv2.CAP_PROP_FPS),
                             (width, height))
    video5 = cv2.VideoWriter(video_nameBor, -1, cap.get(cv2.CAP_PROP_FPS),
                             (width, height))
    video6 = cv2.VideoWriter(video_nameBor2, -1, cap.get(cv2.CAP_PROP_FPS),
                             (width, height))
    for image in imag:
        video.write(cv2.imread(os.path.join(image_folder, image)))
        video2.write(cv2.imread(os.path.join(image_folder, image)))
    for image in imagDen:
        video3.write(cv2.imread(os.path.join(image_folderDen, image)))
        video4.write(cv2.imread(os.path.join(image_folderDen, image)))
    for image in imagBor:
        video5.write(cv2.imread(os.path.join(image_folderBor, image)))
        video6.write(cv2.imread(os.path.join(image_folderBor, image)))
    cv2.destroyAllWindows()
    video.release()
    video2.release()
    video3.release()
    video4.release()
    video5.release()
    video6.release()