예제 #1
0
 def test_rank(self):
     im = np.array([[1, 2, 3], [3, 4, 5], [7, 8, 9]])
     se = np.ones((3, 3))
     out = np.array([[4, 5, 5], [8, 9, 9], [8, 9, 9]])
     im = Image(im)
     imr = im.rank(se)
     nt.assert_array_almost_equal(imr.image, out)
    def test_pixelswitch(self):

        # test monochrome image
        a = np.array([[1, 2], [3, 4]])
        b = np.array([[5, 6], [7, 8]])

        a = Image(a)
        b = Image(b)

        nt.assert_array_almost_equal(
            a.pixelswitch(np.zeros((2, 2)), b).image, b.image)
        nt.assert_array_almost_equal(
            a.pixelswitch(np.ones((2, 2)), b).image, a.image)
        mask = np.array([[0, 1], [1, 0]])
        nt.assert_array_almost_equal(
            a.pixelswitch(mask, b).image, np.array([[5, 2], [3, 8]]))

        # test color image
        a = np.random.randint(0, 255, (2, 2, 3))
        b = np.random.randint(0, 255, (2, 2, 3))
        a = Image(a)
        b = Image(b)
        mask = np.array([[0, 1], [0, 0]])
        out = a.pixelswitch(mask, b)
        nt.assert_array_almost_equal(out.image[0, 0, :], b.image[0, 0, :])
        nt.assert_array_almost_equal(out.image[0, 1, :], a.image[0, 1, :])
        nt.assert_array_almost_equal(out.image[1, 0, :], b.image[1, 0, :])
        nt.assert_array_almost_equal(out.image[1, 1, :], b.image[1, 1, :])
 def test_mono(self):
     # input an image that is not mono
     imname = 'monalisa.png'
     im = Image(imname)
     imm = im.mono()
     self.assertEqual(imm.iscolor, False)
     self.assertEqual(imm.shape, im.size)
예제 #4
0
 def test_endpoint(self):
     im = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
     im = Image(im)
     out = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
     nt.assert_array_almost_equal(im.endpoint().image, out)
예제 #5
0
    def test_showcolorspace(self):

        # test it runs and is the correct shape
        # may also be able to test values of specific coordinates?
        imcs = Image().showcolorspace('xy')
        self.assertEqual(imcs.shape, (451, 401, 3))

        imcs = Image().showcolorspace('ab')
        self.assertEqual(imcs.shape, (501, 501, 3))
예제 #6
0
    def test_colorise(self):

        im = np.array([[1, 2, 3], [1, 2, 3], [1, 3, 3]]) / 10
        im = Image(im)
        out = im.colorise(c=[0, 0, 1])

        # quick element teste
        self.assertEqual(out.rgb[0, 0, 0], 0)
        self.assertEqual(out.rgb[0, 0, 1], 0)
        self.assertEqual(out.rgb[0, 0, 2], 0.1)
    def test_wildcardstr(self):
        # single str with wild card for folder of images
        # print('test_wildcardstr')
        imname = Image('campus/*.png')

        im = Image(imname)
        self.assertEqual(im.numimages, 20)
        self.assertEqual(im.issequence, True)
        self.assertEqual(im.shape, (426, 640, 3))
        self.assertEqual(im.dtype, 'uint8')
        self.assertEqual(im.colororder, 'BGR')
        self.assertEqual(im.numchannels, 3)
    def test_listimage(self):
        # list of Image objects
        # print('test_listimage')
        flowerlist = [str(('flowers' + str(i + 1) + '.png')) for i in range(8)]
        imlist = [Image(flower) for flower in flowerlist]

        im = Image(imlist)

        imfilenamelist = [os.path.split(i.filename)[1] for i in im]
        # imfilenamelist == flowerlist
        self.assertEqual(imfilenamelist, flowerlist)
        self.assertEqual(im.issequence, True)
    def test_image(self):
        # Image object
        # print('test_image')
        imname = 'shark1.png'
        im0 = Image(imname)

        im1 = Image(im0)
        # TODO consider __eq__ to compare Image objects directly im0 == im1
        nt.assert_array_almost_equal(im1.image, im0.image)
        self.assertEqual(im1.filename, im0.filename)
        self.assertEqual(im1.shape, im0.shape)
        self.assertEqual(im1.iscolor, im0.iscolor)
예제 #10
0
    def test_iopen(self):

        im = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0],
                       [0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0],
                       [0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0]])

        out = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                        [0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0],
                        [0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0]])
        im = Image(im)
        se = np.ones((3, 3))
        nt.assert_array_almost_equal(im.open(se).image, out)
예제 #11
0
    def test_humoments(self):

        im = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0],
                       [0, 0, 1, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0],
                       [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0]])
        out = np.array([
            0.184815794830043, 0.004035534812971, 0.000533013844814,
            0.000035606641461, 0.000000003474073, 0.000000189873096,
            -0.000000003463063
        ])
        im = Image(im)
        hu = im.humoments()
        nt.assert_array_almost_equal(argcheck.getvector(hu[0]), out, decimal=7)
    def test_isimage(self):

        # create mini image (Bayer pattern)
        im = np.zeros((2, 2, 3))
        # 0 - red channel, 1 - green channel, 2 - blue channel
        im[0, 0, 0] = 1  # top left = red
        im[0, 1, 1] = 1  # top right = green
        im[1, 0, 1] = 1  # bottom left = green
        im[1, 1, 2] = 1  # bottom right = blue

        # a single grayscale image
        self.assertEqual(Image.isimage(im[:, :, 0].astype(np.float)), True)

        # set type as float, then make sure isimage is true
        self.assertEqual(Image.isimage(im.astype(np.float32)), True)
        self.assertEqual(Image.isimage(im.astype(np.int)), True)
    def test_array(self):
        # test single numpy array
        # print('test_numpyarray')
        imarray = iread('walls-l.png')

        im = Image(imarray[0])
        self.assertEqual(im.shape, (2448, 3264, 3))
        self.assertEqual(im.iscolor, True)
    def test_testpattern(self):
        im = Image()
        tp = im.testpattern('rampx', 10, 2)
        self.assertEqual(tp.shape, (10, 10))

        tp = im.testpattern('rampx', (20, 10), 2)
        self.assertEqual(tp.shape, (10, 20))
        r = np.linspace(0, 1, 10, endpoint=True)
        out = np.hstack((r, r))
        nt.assert_array_almost_equal(tp.image[5, :], out)

        tp = im.testpattern('rampy', (10, 20), 2)
        self.assertEqual(tp.shape, (20, 10))
        nt.assert_array_almost_equal(tp.image[:, 5], out.T)

        tp = im.testpattern('sinx', 12, 1)
        self.assertEqual(tp.shape, (12, 12))
        nt.assert_almost_equal(np.sum(tp.image), 0, decimal=6)
        nt.assert_almost_equal(np.diff(tp.image[:, 2]),
                               np.zeros((11)),
                               decimal=6)

        tp = im.testpattern('siny', 12, 1)
        self.assertEqual(tp.shape, (12, 12))
        nt.assert_almost_equal(np.sum(tp.image), 0, decimal=6)
        nt.assert_almost_equal(np.diff(tp.image[2, :]),
                               np.zeros((11)),
                               decimal=6)

        tp = im.testpattern('dots', 100, 20, 10)
        self.assertEqual(tp.shape, (100, 100))
        # TODO [l,ml,p,c] = ilabel(im);
        # tc.verifyEqual(sum(c), 25);

        tp = im.testpattern('squares', 100, 20, 10)
        self.assertEqual(tp.shape, (100, 100))
        # TODO [l,ml,p,c] = ilabel(im);
        # tc.verifyEqual(sum(c), 25);

        # TODO not yet converted to python:
        tp = im.testpattern('line', 20, np.pi / 6, 10)
        self.assertEqual(tp.shape, (20, 20))
        self.assertEqual(tp.image[10, 0], 1)
        self.assertEqual(tp.image[11, 1], 1)
        self.assertEqual(tp.image[16, 11], 1)
        self.assertEqual(np.sum(tp.image), 17)
예제 #15
0
    def test_dilate(self):
        im = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0]])
        out = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0],
                        [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0]])
        im = Image(im)
        nt.assert_array_almost_equal(im.dilate(np.ones((3, 3))).image, out)

        out = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0],
                        [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0],
                        [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0],
                        [0, 0, 0, 0, 0, 0, 0]])
        nt.assert_array_almost_equal(im.dilate(np.ones((3, 3)), 2).image, out)
예제 #16
0
    def test_iclose(self):

        im = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 0, 0],
                       [0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0]])

        out = np.array([
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [1, 1, 1, 1, 0, 0, 0],  # note the border values
            [1, 1, 1, 1, 0, 0, 0],
            [1, 1, 1, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]
        ])
        im = Image(im)
        se = np.ones((3, 3))
        nt.assert_array_almost_equal(im.close(se).image, out)
    def test_liststr(self):
        # list of image filenames
        # print('test_liststr')
        flowerlist = [str(('flowers' + str(i + 1) + '.png')) for i in range(8)]

        im = Image(flowerlist)
        self.assertEqual(im.numimages, 8)
        self.assertEqual(im.issequence, True)
        imfilenamelist = [i.filename for i in im]
        self.assertTrue(
            all([
                os.path.split(x)[1] == y
                for x, y in zip(imfilenamelist, flowerlist)
            ]))
예제 #18
0
    def test_morph3(self):
        im = Image(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
        out = np.array([[5, 6, 6], [8, 9, 9], [8, 9, 9]])
        nt.assert_array_almost_equal(
            im.morph(np.ones((3, 3)), oper='max', opt='none').image, out)

        out = np.array([[1, 1, 2], [1, 1, 2], [4, 4, 5]])
        nt.assert_array_almost_equal(
            im.morph(np.ones((3, 3)), oper='min', opt='replicate').image, out)

        # simple erosion
        im = Image(np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]], dtype=np.uint8))
        out = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]], dtype=np.uint8)
        nt.assert_array_almost_equal(
            im.morph(se=np.ones((3, 3)), oper='min').image, out)
    def test_str(self):
        # single color image as str
        # print('test_str')
        imname = 'monalisa.png'

        im = Image(imname)
        # check attributes
        nt.assert_array_equal(im.shape, (700, 677, 3))
        self.assertEqual(os.path.split(im.filename)[1], imname)
        self.assertEqual(im.iscolor, True)
        self.assertEqual(im.dtype, 'uint8')
        self.assertEqual(im.width, 677)
        self.assertEqual(im.height, 700)
        self.assertEqual(im.issequence, False)
        self.assertEqual(im.ndim, 3)
        self.assertEqual(im.numimages, 1)
        self.assertEqual(im.colororder, 'BGR')
        self.assertEqual(im.numchannels, 3)
    def test_listarray(self):
        # test list of arrays
        # print('test_listarray')
        flowerlist = [str(('flowers' + str(i + 1) + '.png')) for i in range(8)]
        imlist = [iread(i)[0] for i in flowerlist]
        # concatenate list of images into a stack of images
        imlistexp = [
            np.expand_dims(imlist[i], axis=3) for i in range(len(imlist))
        ]
        imstack = imlistexp[0]
        for i in range(1, 8):
            imstack = np.concatenate((imstack, imlistexp[i]), axis=3)

        im = Image(imstack)
        self.assertEqual(im.numimages, 8)
        self.assertEqual(im.shape, (426, 640, 3))
        self.assertEqual(im.dtype, 'uint8')
        self.assertEqual(im.colororder, 'BGR')
        self.assertEqual(im.numchannels, 3)
        self.assertEqual(im.issequence, True)
예제 #21
0
    def test_erode(self):
        im = np.array([[1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0],
                       [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0],
                       [0, 0, 0, 0, 0, 0]])
        im = Image(im)
        out = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0]])
        nt.assert_array_almost_equal(im.erode(np.ones((3, 3))).image, out)

        im = np.array([[1, 1, 1, 0], [1, 1, 1, 0], [0, 0, 0, 0]])
        im = Image(im)
        out = np.array([[1, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
        nt.assert_array_almost_equal(
            im.erode(np.ones((3, 3)), opt='replicate').image, out)
    def test_options(self):

        imname = 'monalisa.png'
        im = Image(imname, colororder='BGR')

        # check predicatives
        self.assertEqual(im.issequence, False)
        self.assertEqual(im.isfloat, False)
        self.assertEqual(im.isint, True)
        self.assertEqual(isinstance(im, Image), True)
        nt.assert_array_equal(im.bgr.shape, im.shape)
        nt.assert_array_equal(im.rgb.shape, im.shape)
        self.assertEqual(im.size, (700, 677))

        # check one element for rgb vs bgr ordering
        v = round(im.shape[0] / 2)  # rows
        u = round(im.shape[1] / 2)  # cols
        bgr = im.bgr[v, u, :]
        nt.assert_array_equal(im.rgb[v, u, :], bgr[::-1])

        self.assertEqual(im.isrgb, False)
        self.assertEqual(im.isbgr, True)

        self.assertEqual(im.iscolor, True)
    def test_window(self):
        im = np.array([[3, 5, 8, 10, 9], [7, 10, 3, 6, 3], [7, 4, 6, 2, 9],
                       [2, 6, 7, 2, 3], [2, 3, 9, 3, 10]])
        im = Image(im)
        se = np.ones((1, 1))
        # se must be same number of dimensions as input image for scipy
        # TODO maybe detect this in im.window and perform this line?

        # test with different input formats
        nt.assert_array_almost_equal(im.window(se, np.sum).image, im.image)
        nt.assert_array_almost_equal(
            im.int('uint8').window(se, np.sum).image, im.image)
        nt.assert_array_almost_equal(
            im.int('uint16').window(se, np.sum).image, im.image)

        se = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
        out = np.array([[43, 47, 57, 56, 59], [46, 43, 51, 50, 57],
                        [45, 48, 40, 39, 31], [33, 40, 35, 49, 48],
                        [22, 40, 36, 53, 44]])
        nt.assert_array_almost_equal(im.window(se, np.sum).image, out)
예제 #24
0
    def test_morph1(self):

        # test simple case
        im = Image(np.array([[1, 2], [3, 4]]))
        se = 1
        nt.assert_array_almost_equal(im.morph(se, 'min').image, im.image)
        nt.assert_array_almost_equal(im.morph(se, 'max').image, im.image)
        nt.assert_array_almost_equal(
            im.morph(se, oper='min', opt='replicate').image, im.image)
        nt.assert_array_almost_equal(
            im.morph(se, oper='min', opt='none').image, im.image)

        # test different input formats
        nt.assert_array_almost_equal(
            im.int('uint8').morph(se, 'min').image, im.image)
        nt.assert_array_almost_equal(
            im.int('uint16').morph(se, 'min').image, im.image)
        nt.assert_array_almost_equal(im.float().morph(se, 'min').image * 255,
                                     im.image)

        im = np.array([[1, 0, 1], [0, 1, 0], [1, 1, 0]])
        im = Image(im.astype(bool))
        nt.assert_array_almost_equal(im.morph(se, 'min').image, im.image)
예제 #25
0
    def test_gamma(self):

        a = Image(np.array([[0.4]]))
        g = a.gamma_encode(0.5)
        nt.assert_array_almost_equal(g.image * g.image, a.image)

        a = Image(np.array([[64.0]]))
        g = a.gamma_encode(0.5)
        nt.assert_array_almost_equal(g.image * g.image, a.image)

        # test for shape
        g = a.gamma('srgb')
        self.assertEqual(g.shape, a.shape)

        a = Image(np.random.rand(5, 5))
        g = a.gamma(0.5)
        nt.assert_array_almost_equal(g.shape, a.shape)
        nt.assert_array_almost_equal(g.gamma(2).image, a.image)

        a = Image(np.random.rand(5, 5, 3))
        g = a.gamma(0.5)
        nt.assert_array_almost_equal(g.shape, a.shape)
예제 #26
0
import code
# import machinevisiontoolbox as mvtb
# from machinevisiontoolbox import Image, Blob
from machinevisiontoolbox.Image import Image

# # im = Image("machinevisiontoolbox/images/flowers?.png")

if True:
    im = Image("flowers1.png")
    # im.disp()
    print(im)

    red = im.red()
    blue = im.blue()
    print(red)

    # im.disp(block=False)
    # red.disp()

    grey = im.mono()
    print(grey)
    # grey.disp()

print(im.isint)
im.stats()
z = im.float()**2
print(z)
z.stats()
z.disp()

z = im * 0.5
예제 #27
0
        out = []
        for im in self:

            if im.iscolor:
                R = gamma_decode(m.red, gamma)
                G = gamma_decode(im.green, gamma)
                B = gamma_decode(im.blue, gamma)
                out.append(np.dstack((R, G, B)))
            else:
                out.append(gamma_decode(im.image, gamma))

        return self.__class__(out)


# --------------------------------------------------------------------------#
if __name__ == '__main__':

    # test run ImageProcessingColor.py
    print('ImageProcessingColor.py')

    from machinevisiontoolbox.Image import Image

    im = Image('monalisa.png')
    im.disp()

    imcs = Image.showcolorspace()
    imcs.disp()

    import code
    code.interact(local=dict(globals(), **locals()))
    def test_similarity(self):

        a = np.array([[0.9280, 0.3879, 0.8679], [0.1695, 0.3826, 0.7415],
                      [0.8837, 0.2715, 0.4479]])
        a = Image(a)

        eps = 2.224e-16 * 100
        # self.assertAlmostEqual(a.sad(a), eps)
        self.assertEqual(abs(a.sad(a)) < eps, True)
        self.assertEqual(abs(a.sad(Image(a.image + 0.1))) < eps, False)

        self.assertEqual(abs(a.zsad(a)) < eps, True)
        self.assertEqual(abs(a.zsad(Image(a.image + 0.1))) < eps, True)

        self.assertEqual(abs(a.ssd(a)) < eps, True)
        self.assertEqual(abs(a.ssd(Image(a.image + 0.1))) < eps, False)

        self.assertEqual(abs(a.zssd(a)) < eps, True)
        self.assertEqual(abs(a.zssd(Image(a.image + 0.1))) < eps, True)

        self.assertEqual(abs(1 - a.ncc(a)) < eps, True)
        self.assertEqual(abs(1 - a.ncc(Image(a.image * 2))) < eps, True)

        self.assertEqual(abs(1 - a.zncc(a)) < eps, True)
        self.assertEqual(abs(1 - a.zncc(Image(a.image + 0.1))) < eps, True)
        self.assertEqual(abs(1 - a.zncc(Image(a.image * 2))) < eps, True)
예제 #29
0
                    r'({0})  area={1:.1f}, \
                  cent=({2:.1f}, {3:.1f}), \
                  orientation={4:.3f}, \
                  b/a={5:.3f}, \
                  touch={6:d}, \
                  parent={7}, \
                  children={8}', i, self._area[i], self._uc[i], self._vc[i],
                    self._orientation[i], self._aspect[i], self._touch[i],
                    self._parent[i], self._children[i]))


if __name__ == "__main__":

    # read image
    from machinevisiontoolbox.Image import Image
    im = Image(cv.imread('images/multiblobs.png', cv.IMREAD_GRAYSCALE))

    # call Blobs class
    b = Blob(image=im)

    # plot image
    # plot centroids of blobs
    # label relevant centroids for the labelled blobs
    # import random as rng  # for random colors of blobs
    rng.seed(53467)

    drawing = np.zeros((im.shape[0], im.shape[1], 3), dtype=np.uint8)
    colors = [None] * len(b)
    icont = [None] * len(b)
    for i in range(len(b)):
        icont[i] = i
예제 #30
0
 def image(self, newimage):
     self._image = Image(newimage)