예제 #1
0
def show_crops(crops, fig_n):
    fig = plt.figure(fig_n)
    ax1 = fig.add_subplot(131)
    ax2 = fig.add_subplot(132)
    ax3 = fig.add_subplot(133)
    ax1.imshow(np.uint8(crops[0, :, :, :]))
    ax2.imshow(np.unit8(crops[1, :, :, :]))
    ax3.imshow(np.unit8(crops[2, :, :, :]))
    plt.ion()
    plt.show()
    plt.pause(0.001)
예제 #2
0
 def test_101(self):
     # 10.1图像加法
     # 函数cv2.add() 将两幅图像进行加法运算,当然也可以直接使
     # 用numpy,res=img1+img。两幅图像的大小,类型必须一致,或者第二个
     # 图像可以使一个简单的标量值
     # OpenCV 的加法是一种饱和操作,而Numpy 的加法是一种模操作。
     x = np.unit8([250])
     y = np.unit8([10])
     # 250+10 = 260 => 255
     print(cv2.add(x, y))
     # # 250+10 = 260 % 256 = 4
     print(x + y)
예제 #3
0
def decode_idx3_ubyte(idx3_ubyte_file, saveflag=False):

    status = 'save/'

    with open(idx3_ubyte_file, 'rb') as f:
        buf = f.train_image.read()

    index = 0
    magic, imagenum, rows, cols = struct.unpack_from('>IIII', buf, index)
    index += struct.calcsize('>IIII')
    image = np.empty((imagenum, rows, cols))
    image_size = rows * cols

    # define how many numbers(size of photo) to get for one time
    fmt = '>' + str(image_size) + 'B'

    for i in range(100):
        im = struct.unpack_from(fmt, buf, index)
        im = np.reshape(im, [rows, cols])
        image[i] = np.array(im)

        if saveflag:
            im = image.from_array(np.unit8(image[i]))
            im.save(status + str(i) + '.png')

        index += struct.calcsize(fmt)

    return image, imagenum
예제 #4
0
def pipeline(img, s_thresh=(100, 255), sx_thresh=(15, 255)):
    img = undistort(img)
    img = np.copy(img)
    hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS).astype(np.float)
    l_channel = hls[:, :, 1]
    s_channel = hls[:, :, 2]
    h_channel = hls[:, :, 0]
    # Sobel x

    sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 1)  # Take the derivative in x
    abs_sobelx = np.absolute(
        sobelx)  #Absolute x derivative to accentuate lines away
    scaled_sobel = np.unit8(255 * abs_sobelx / np.max(abs_sobelx))

    # Threshold color channel
    s_binary = np.zeros_like(scaled_sobel)
    sxbinary[(scaled_sobel >= sx_thresh[0])
             & (scaled_sobel <= sx_thresh[1])] = 1
    # Threshold color channel
    s_binary = np.zeros_like(s_channel)
    s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
    color_binary = np.dstack(
        (np.zeros_like(sxbinary), sxbinary, s_binary)) * 255

    combined_binary = np.zeros_like(sxbinary)
    combined_binary[(s_binary == 1) | (sxbinary == 1)] = 1
    return combined_binary
예제 #5
0
 def depth_estm(self,li,ri):
     stereo = cv2.StereoBM_create(numDisparities=16, blockSize=5)
     disparity = stereo.compute(li,ri)
     min_val = disparity.min()
     max_val = disparity.max()
     disparity = np.unit8(6400*(disparity - min_val)/ (max_val - min_val))
     cv2.imshow('disparittet',np.hstack((imgLeft, imgRight, disparity)))
     cv2.waitKey(0)
     cv2.destroyAllWindows()
예제 #6
0
def convert_to_8bit(img, auto_scale=True):

    # Convert to 8 bit and autoscale
    if auto_scale:

        # result = np.float32(img)-np.median(img)
        # max_val1 = np.max(img)
        # max_val2 = np.max(result)
        # result[result < 0] = 0
        # result = result/(0.5*max_val1+0.5*max_val2)
        #
        # bch = result[:,:,0]
        # gch = result[:,:,1]
        # rch = result[:,:,2]
        # b_avg = np.mean(bch)
        # g_avg = np.mean(gch)
        # r_avg = np.mean(rch)
        # avg = np.mean(np.array([b_avg,g_avg,r_avg]))
        # #print "R: " + str(r_avg) + ", G: " + str(g_avg) + ", B: " + str(b_avg)
        # bch = bch*1.075
        # rch = rch*0.975
        # gch = gch*0.95
        # # bch = bch*avg/b_avg
        # # rch = rch*avg/r_avg
        # # gch = gch*avg/g_avg
        # # b_avg = np.mean(bch)
        # # g_avg = np.mean(gch)
        # # r_avg = np.mean(rch)
        # #print "New R: " + str(r_avg) + ", G: " + str(g_avg) + ", B: " + str(b_avg)
        # result[:,:,0] = bch
        # result[:,:,1] = gch
        # result[:,:,2] = rch

        #result = result/np.max(result)

        result = np.float32(img) - np.min(img)
        result[result < 0.0] = 0.0
        if np.max(img) != 0:
            result = result / np.max(img)

        img_8bit = np.uint8(255 * result)
    else:
        img_8bit = np.unit8(img)

    return img_8bit
예제 #7
0
def displayGradcam(image_array,
                   heatmap,
                   cam_path: Optional[str] = None,
                   alpha: float = 0.4):
    """
    # Arguments:\n
        cam_path: None or string. The file name with directory name.\n

    # Details:\n
        - The superimposed image will be not saved when cam_path=None.\n
    """
    # argument check
    img = image_array
    if len(img.shape) == 4:
        img = img.reshape(img.shape[1:])

    # Rescale heatmap to a range 0-255
    heatmap = np.uint8(255 * heatmap)
    img = np.unit8(255 * img)

    # Use jet colormap to colorize heatmap
    jet = cm.get_cmap("jet")

    # Use RGB values of the colormap
    jet_colors = jet(np.arange(256))[:, :3]
    jet_heatmap = jet_colors[heatmap]

    # Create an image with RGB colorized heatmap
    jet_heatmap = tf.keras.preprocessing.image.array_to_img(jet_heatmap)
    jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
    jet_heatmap = tf.keras.preprocessing.image.img_to_array(jet_heatmap)

    # Superimpose the heatmap on original image
    superimposed_img = jet_heatmap * alpha + img
    superimposed_img = tf.keras.preprocessing.image.array_to_img(
        superimposed_img)

    # Save the superimposed image
    if cam_path is not None:
        superimposed_img.save(cam_path)

    # Display Grad CAM
    display(Image(cam_path))
예제 #8
0
def crossover_uniform(parents, childrenSize):
    crossoverPointIndex = np.arrange(0, np.unit8(childrenSize[1]),1, dtype = np.uint8) #get all Indexes
    corssoverPointIndex1 = np.random.randint(0,np.uint8(childrenSize[1]),np.uint8(childrenSize[1]/2)) # select half of Indexes randomly
    crossoverPointIndex2 = np.array(list(set(crossoverPointIndex) - set(crossoverPointIndex1))) #select leftover Indexes
    children = np.empty(childrenSize)
    '''
    Create child by choosing parameters from two parents selected using new_parent_selection function. The parameter values
    will be picked from the indexes, which were randomly selected above.
    '''
    for i in range(childrenSize[0]):
        #find parent 1 index
        parent1_index = i % parents.shape[0]
        #find parent 2 index
        parent2_index = (i+1) % parents.shape[0]
        #insert paramaters based on random selected indexes in parent 1
        children[i, crossoverPointIndex1] = parents[parents1_index, crossoverPointIndex1]
        #Insert parameters based on random selected indexes in parent 1
        children[i, crossoverPointIndex2] = parents[parents2_index, crossoverPointIndex2]
        return children
예제 #9
0
    def grad_cam(self):
        """
        """
        # preproce
        # 本来は複数画像に対してmodel.predictを行うため、次元を拡張する必要がある
        # see https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html
        X = np.expand_dims(self.x, axis=0)
        X = X.astype('float32')
        X = X / 255.0

        predictions = self.model.predict(X)
        class_idx = np.argmax(predictions[0])
        class_output = self.model.output[:, class_idx]

        # 勾配を取得
        conv_output = self.model.get_layer(self.layer_name).output
        # see https://keras.io/ja/backend/
        grads = K.gradients(class_output, conv_output)
        # model.inputを入力すると、 conv_outputとgradsを返す関数
        gradient_function = K.function([self.model.input], [conv_output, grads])

        output, grads_val = gradient_function(X)
        output, grads_val = output[0], grads[0]

        # 重みを平均化して、レイヤーのおアウトプットに乗じる
        weights = np.mean(grads_val, axis=(0, 1))
        cam = np.dot(output, weights)

        # 画像化してヒートマップにして合成する
        cam = cv2.resize(cam, (200, 200), cv2.INNER_LINEAR)
        cam = np.maximum(cam, 0)
        cam = cam / cam.max()

        # モノクロ画像に疑似的に色を付ける
        g_cam = cv2.applyColorMap(np.unit8(255 * cam), cv2.COLORMAP_JET)
        # 色をRGBに変換
        g_cam = cv2.cvtColor(g_cam, cv2.COLOR_BGR2RGB)
        # 元画像に合成
        g_cam = (np.float32(g_cam) + x / 2)

        return g_cam
예제 #10
0
def convert_to_8bit(img, auto_scale=True):
    """Convert image to 8-bits with scaling

    Args:
        img (ndarray): the image to convert
        auto_scale (bool, optional): Auto-scale the image. Defaults to True.

    Returns:
        ndarray: The image converted to 8-bits
    """
    if auto_scale:
        result = np.float32(img)-np.min(img)
        result[result<0.0] = 0.0
        if np.max(img) != 0:
            result = result/np.max(img)

        img_8bit = np.uint8(255*result)
    else:
        img_8bit = np.unit8(img)

    return img_8bit
예제 #11
0
# 모델 훈련하기
model.fit(X_train, Y_train, batch_size=32, epochs=50)

# # 모델 평가하기
# score = model.evaluate(X_test,Y_test)
# print('loss=', score[0])
# print('accuracy=',score[1])

# 모델 평가하기
pre = model.predict(X_test)
for i, v in enumerate(pre):
    pre_ans = v.argmax()
    ans = Y_test[i].argmax()
    dat = X_test[i]
    if ans == pre_ans:
        continue

    print("[NG]", categories[pre_ans], "!=", categories[ans])
    print(v)
    if not os.path.exists("./image/error"):
        os.mkdir("./image/error")
    fname = "image/error/" + str(i) + "-" + categories[pre_ans] + "-ne-" + categories[ans] + ".png"
    dat *= 256
    img = Image.fromarray(np.unit8(dat))
    img.save(fname)

score = model.evaluate(X_test, Y_test)
print('loss=', score[0])
print('accuracy=', score[1])
예제 #12
0
    def encode_four_bits(self, a):
        num = 0
        n = len(self.data)
        # 先编码第一个数
        # 小于0
        if self.data[0] < 0:
            if -self.data[0] < a:  # 如果小于量化因子,直接编码为-1
                num = 15
            else:
                num = 15 - (
                    np.uint8(int(0.5 + abs(np.log(-self.data[0] / a))) << 4) >>
                    4)  # 当数据为负时,符号位设置为1
            if num < 8:  # 下溢,编码为8
                self.encode_four.append(8)
            else:  # 正常
                self.encode_four.append(num)
            self.four_decode.append(-a * np.exp(
                (15 - self.encode_four[0])))  # 负数,乘以量化因子a
        # 大于等于0
        else:
            if self.data[0] < a:  # 小于量化因子,直接编码为1
                num = 0
            else:
                num = np.unit8(int(0.5 +
                                   abs(np.log(self.data[0] / a))) << 4) >> 4
            if num > 7:  # 上溢,编码为7
                self.encode_four.append(7)
            else:  # 正常
                self.encode_four.append(num)
            self.four_decode.append(
                a * np.exp(self.encode_eight[0]))  # 正数解码,乘以量化因子a

        # 编码剩余的点
        for i in range(1, n):
            num = 0
            # 与解码后的数据相减的差
            d = self.data[i] - self.four_decode[i - 1]
            if d < 0:  # 差为负
                if -d < a:  # 小于量化因子
                    num = 15
                else:
                    num = 15 - (np.uint8(int(0.5 + abs(np.log(-d / a))) << 4)
                                >> 4)  # 当数据为负时,符号位设置为1
                if num < 8:  # 下溢
                    self.encode_four.append(8)
                else:
                    self.encode_four.append(num)
                self.four_decode.append(self.four_decode[i - 1] - a * np.exp(
                    (15 - self.encode_four[i])))  # 负数,乘以量化因子a
            else:  # 差非负
                if d < a:  # 小于量化因子
                    num = 0
                else:
                    num = np.uint8(int(0.5 +
                                       abs(np.log(d / a + 1e-5))) << 4) >> 4
                if num >= 8:  # 上溢
                    self.encode_four.append(7)
                else:
                    self.encode_four.append(num)
                self.four_decode.append(self.four_decode[i - 1] +
                                        a * np.exp(self.encode_four[i]))

        f = open('1_4bit.dpc', 'wb')  # 打开写入文件
        # 防止奇数个时溢出,故减一
        length = len(self.encode_four)
        for i in range(0, length - 1, 2):
            # 两个一起写
            x = (self.encode_four[i] << 4) + self.encode_four[i + 1]
            f.write(np.uint8(x))
        if length % 2 == 1:
            x = self.encode_four[length - 1] << 4
            f.write(np.uint8(x))
        f.close()
예제 #13
0
#print(last_conv_layer.output.shape)
grads = K.gradients(africa_elephant_output,
                    last_conv_layer.output)[0]  #梯度是对变量的"导数和"
print(grads.shape)

#%%
import matplotlib.pyplot as plt
pooled_grads = K.mean(grads, axis=(0, 1, 2))  #形状为(512,)的向量。每个元素是特定特征通道图的平均梯度大小
iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, last_conv_layer_value = iterate([x])  # 得到"梯度"和"输出层"
for i in range(512):
    last_conv_layer_value[:, :, i] *= pooled_grads_value[
        i]  # 一共512个通道,每个通道*这个通道对于"大象"类别的重要程度

hearmap = np.mean(last_conv_layer_value, axis=-1)  #得到逐通道平均值为类激活的热力图
hearmap = np.maximum(hearmap, 0)  #负的舍弃为0
hearmap /= np.max(hearmap)  #除以最大值化为0-1的值
plt.matshow(hearmap)
plt.show()

#%%
#将热力图与原始图像叠加

import cv2
img = cv2.imread(img_path)
hearmap = cv2.resize(hearmap, (img.shape[1], img.shape[0]))
hearmap = np.unit8(255 * hearmap)  #转化为RGB格式
hearmap = cv2.applyColorMap(hearmap, cv2.COLORMAP_JET)  #将热力图应用于原始图像
superimposed_img = hearmap * 0.4 + img  #这里的0.4是热力图强度因子
cv2.imshow(superimposed_img)
# OpenCV : HSV = 0~ 255

# BGR to HSV 변환
import numpy as np
import cv2

color = [255, 0, 0]  #Blue
pixel = np.unit8([[color]])  # cvtColor함수의 입력으로 사용하기 위해 : 한 픽셀로 구성된 이미지로 변환

hsv = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)  # cvtColor 함수를 이용하여 HSV 색공간으로 변환
hsv = hsv[0][0]  # hsv 값 출력을 위해 pixel 값만 가져오기

print('bgr: ', color)
print('hsv: ', hsv)

# Result
# bgr : [255, 0, 0]
# hsv: [120 255 255] # hue 값인 120 에서 +-10을 범위로 잡는다 (110~130)
# Saturation과 Value 값은 항상 일정한 범위값으로 사용하기 때문에 값을 무시한다.

# 이미지에서 파란색을 검출하는 코드
import cv2

img_color = cv2.imread('image.jpg')  # color 검출할 이미지
height, width = img_color.shape[:2]

img_hsv = cv2.cvtColor(img_color, cv2.COLOR_BGR2HSV)

# 범위를 정하여 hsv 이미지에서 원하는 색영역을 binary 이미지로 생성
lower_blue = (
    120 - 10, 30, 30
예제 #15
0
import cv2
import numpy as np
img = cv2.imread('./data/lena.jpg')
# img = cv2.imread('./data/lena.jpg', cv2.IMREAD_GRAYSCALE)
print('img.dmin=', img.ndim)
print('img.shape=', img.shape)
print('img.dtype=', img.dtype)
# np.bool, np.uint16, np.unit32, np.float32, np.float64, np.complex64
img = img.astype(np.int32)
print('img.dtype=', img.dtype)
img = np.unit8(img)
print('img.dtype=', img.dtype)
예제 #16
0
    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([130, 255, 255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)

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

cv2.destroyAllWindows()

# How to find HSV values to track?
green = np.unit8([[[0, 255, 0]]])
hsv_green = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print(hsv_green)
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 24 12:04:23 2020

@author: Usuario
"""

import cv2
import numpy as np

a = np.zeros((100, 50))
b = np.ones((100, 50))

img = np.unit8(255 * np.concatenate(a, b, axis=1))

gx = cv2.Sobel(img, cv2.CV_64F, 1, 0, 5)
gy = cv2.Sobel(img, cv2.CV_64F, 1, 1, 5)

mag, ang = cv2.cartToPolar(gx, gy)

gx = cv2.convertScaleAbs(gx)
gy = cv2.convertScaleAbs(gy)

mag = cv2.convertScaleAbs(mag)
ang = cv2.convertScaleAbs(mag)

cv2.waitKey(0)
cv2.destroyAllWindows()
예제 #18
0
------- PIL -> NumPy -------
import numpy as np
from PIL import Image

img = Image.opne('img.jpg')
np.array(img)
----------------------------


------- Numpy -> PIL -------
import numpy as np
from PIL import Image

arr = np.zeros((256,256,3))
Image.fromarray(np.unit8(arr))
----------------------------


------ Numpy -> PyTorch ------
import numpy as np
import torch

arr = np.zeros((256,256,3))
torch.from_numpy(arr)
------------------------------


------ PyTorch -> PIL ------
import torch
import torchvision
예제 #19
0
pooled_grads_value, conv_layer_output_value = iterate([x])
for i in range(512):
    conv_layer_output_value[:, :, i] *= pooled_grads_value[i]

heatmap = np.mean(conv_layer_output_value, axis=-1)

# 热力图后处理
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
plt.matshow(heatmap)

# 将热力图与原始图像叠加
import cv2
img = cv2.imread(img_path)
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = np.unit8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = heatmap * 0.4 + img
plt.show()
cv2.imwrite('', superimposed_img)

# import tensorflow as tf
# import numpy as np
#
# trX = np.linspace(-1, 1, 100)
# trY = trX + np.random.randn(*trX.shape) * 0.33
#
# teX = np.linspace(2, 3, 100)
# teY = teX
#
# X = tf.placeholder(tf.float32, [100, ], name="input_x")
예제 #20
0
def img_show(img):
    pil_img = Image.fromarray(np.unit8(img))
    plit_img.show()
예제 #21
0
[colF, ftSize,
 ftWidth] = [level + 2, (level + 2) * 0.15, 1 + int((level + 2) / 5)]
charSetGray = [
    '.', ';', '-', ':', '!', '>', '7', '?', 'C', 'O', '$', 'Q', 'H', 'N', 'M',
    'M'
]
cap = cv2.VideoCapture(VIDEOPATH)
sourceWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
sourceHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
sourceFrameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
sourceFPS = int(cap.get(cv2.CAP_PROP_FPS))
outWidth = 1280
outHeight = int(sourceHeight * (outWidth / sourceWidth))
print('Prepare paint......')
outImg = cv2.cvtColor(
    np.unit8([[0 for i in range(outWidth)] for j in range(outHeight)]),
    cv2.COLOR_BGR2GRAY)
fcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
writer = cv2.VideoWriter(VIDEOPATH, fcc, sourceFPS, (outWidth, outHeight))
for i in range(sourceFrameCount):
    writer.write(outImg)
writer.release()
capOut = cv2.VideoCapture(VIDEOPATH)
outImgs = []
for fr in range(sourceFrameCount):
    _, outImg = capOut.read()
    outImgs.append(outImg)
print('Canvas ready\n')
for fr in range(sourceFrameCount):
    _, srcImg = cap.read()
    srcArray = np.uint8(srcImg)
예제 #22
0
파일: t6.py 프로젝트: zhang1998/pdfuse
img = cv2.imread('text.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2BGRA )

adaptive_threshold = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
                                           cv2.THRESH_BINARY_INV, 11, 2)

kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=2)


sure_bg=cv2.dilate(opening,kernel,iterations=3)

dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)

ret,sure_fg=cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

sure_fg=np.unit8(sure_fg)
unknown= cv2.subtract(sure_bg,sure_fg)

ret,markers = cv2.connectedComponents(sure_fg)

markers=markers+1
markers[unknown==255]=0

markers = cv2.watershed(img,markers)

img[markers==-1 ] = [255,0,0]
plt.imshow(img)
plt.show()

예제 #23
0
파일: awmf.py 프로젝트: vinsmokemau/Imaging
"""."""
from skimage import data, io, color

import numpy as np
import matplotlib.pyplot as plt
import random
from skimage.morphology import disk
from skimage.filter import rank

img = np.unit8(color.rgb2gray(io.imread('img/chest.jpg')) * 255)
plt.figure(1)
plt.subplot(131)
plt.imshow(img, cmap='gray')
plt.title('Original Image')
plt.suptitle('Image Enhancement with AWMF')

row, columns = img.shape
snp = 0.5
amount = 0.9
imr = np.copy(img)
num_salt = np.ceil(amount * img.size * snp)
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in imr.shape]
imr[coords] = 255
num_pepper = np.ceil(amount * imr.size * (1 - snp))
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in imr.shape]
imr[coords] = 0

plt.subplot(132)
plt.imshow(imr, cmap="gray")
plt.title('S&P noisy Image')