使用np.fromfile()方法

时间:2017-11-05 01:34:03

标签: python numpy matplotlib python-imaging-library hough-transform

我知道RAW图像是交错的,每个像素都包含每个R,G,B分量的值。所以我写了下面的代码来读取RGB像素值并重新整形图像数组以使其成为RGB图像。之后我将图像转换为二进制图像以进一步处理。转换后,当我试图显示图像时,它似乎包含噪音。任何人都可以帮我消除噪音吗?

Python版本:3.6

代码:

from scipy import misc
from scipy import ndimage
import numpy as np
import matplotlib.pyplot as plt
import math
from PIL import Image
import rawpy
import imageio

def convert_to_binary(img):
    threshold = 150
    img[img <= 30] = 0
    img[img > 30] = 255
    return img

def sobel_test(im):
    dx = ndimage.sobel(im, 0)  # horizontal derivative
    dy = ndimage.sobel(im, 1)  # vertical derivative
    mag = np.hypot(dx, dy)  # magnitude
    print(np.max(mag))
    mag *= 255.0 / np.max(mag)  # normalize (Q&D)
    print(mag)
    misc.imsave('sobel.jpg', mag)
    return mag


A = np.fromfile("ABC.raw", dtype='int8', sep="")
A = np.reshape(A, (756, 1008, 3))
print("A.shape: %d", A.shape)
print("A: ", A)
print("A[0]: ", A[0])
img_gray = np.dot(A[..., :3], [0.30, 0.59, 0.11])
print("img_gray :", img_gray)
print("img_gray shape: ", img_gray.shape)
print("img_gray size: ", img_gray.size)

sob_img = sobel_test(img_gray)
binary_img = convert_to_binary(sob_img)
plt.imshow(binary_img, cmap="gray")
plt.show()

INPUT RAW图片:https://drive.google.com/open?id=12LcQZyYWMg9_6T_n3gjy7kBadRGNMDdr

输出图像

0 个答案:

没有答案