Exemple #1
0
    def triangulateAdvanced(self):
        triangles = [[
            CalcRealCoords(const.dimSize, self.pointSets[trIdx][pIdx])
            for pIdx in range(3)
        ] for trIdx in range(2)]

        tr1Dists = [
            CalcDistance(triangles[0][pIdx1], triangles[0][pIdx2])
            for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2])
        ]
        tr2Dists = [
            CalcDistance(triangles[1][pIdx1], triangles[1][pIdx2])
            for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2])
        ]

        rcSum = [0, 0]
        rotCenters = []
        for idx1 in range(3):
            for idx2 in range(idx1 + 1, 3):
                # print(triangles[0][idx1], triangles[0][idx2])
                # print(triangles[1][idx1], triangles[1][idx2])
                rotCenter = tr.FindRotationCenter(
                    [triangles[0][idx1], triangles[0][idx2]],
                    [triangles[1][idx1], triangles[1][idx2]])
                rotCenters.append(rotCenter)
                print(rotCenter)
                rcSum = list(np.array(rcSum) + np.array(rotCenter))

        rotCenterAvg = list(np.array(rcSum) / 3.0)
        # print(rotCenterAvg)

        rcShift = [-int(rc) for rc in rotCenterAvg]
        rcShift.reverse()
        img1 = imsup.CopyImage(self.image.prev)
        img2 = imsup.CopyImage(self.image)

        # ten padding trzeba jednak dodac ze wszystkich stron
        bufSz = max([abs(x) for x in rcShift])
        # dirV = 't-' if rcShift[1] > 0 else '-b'
        # dirH = 'l-' if rcShift[0] > 0 else '-r'
        dirs = 'tblr'
        # img1Pad = imsup.PadImage(img1, bufSz, 0.0, dirV+dirH)
        # img2Pad = imsup.PadImage(img2, bufSz, 0.0, dirV+dirH)
        img1Pad = imsup.PadImage(img1, bufSz, 0.0, dirs)
        img2Pad = imsup.PadImage(img2, bufSz, 0.0, dirs)

        img1Rc = cc.ShiftImage(img1Pad, rcShift)
        img2Rc = cc.ShiftImage(img2Pad, rcShift)
        # cropCoords = imsup.MakeSquareCoords(imsup.DetermineCropCoords(img1Rc.width, img1Rc.height, rcShift))
        # img1Rc = imsup.CropImageROICoords(img1Rc, cropCoords)
        # img2Rc = imsup.CropImageROICoords(img2Rc, cropCoords)
        img1Rc = imsup.CreateImageWithBufferFromImage(img1Rc)
        img2Rc = imsup.CreateImageWithBufferFromImage(img2Rc)
        imsup.SaveAmpImage(img1Rc, 'a.png')
        imsup.SaveAmpImage(img2Rc, 'b.png')

        rotAngles = []
        for idx, p1, p2 in zip(range(3), triangles[0], triangles[1]):
            p1New = CalcNewCoords(p1, rotCenterAvg)
            p2New = CalcNewCoords(p2, rotCenterAvg)
            triangles[0][idx] = p1New
            triangles[1][idx] = p2New
            rotAngles.append(CalcRotAngle(p1New, p2New))

        rotAngleAvg = np.average(rotAngles)

        mags = [dist1 / dist2 for dist1, dist2 in zip(tr1Dists, tr2Dists)]
        magAvg = np.average(mags)

        # tr1InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr1Dists, tr1Dists[-1:] + tr1Dists[:-1], tr1Dists[-2:] + tr1Dists[:-2]) ]
        # tr2InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr2Dists, tr2Dists[-1:] + tr2Dists[:-1], tr2Dists[-2:] + tr2Dists[:-2]) ]

        # print('---- Triangle 1 ----')
        # print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr1Dists) ])
        # print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr1InnerAngles) ])
        # print('---- Triangle 2 ----')
        # print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr2Dists) ])
        # print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr2InnerAngles) ])
        print('---- Magnification ----')
        print([
            'mag{0} = {1:.2f}x\n'.format(idx + 1, mag)
            for idx, mag in zip(range(3), mags)
        ])
        print('---- Rotation ----')
        print([
            'phi{0} = {1:.0f} deg\n'.format(idx + 1, angle)
            for idx, angle in zip(range(3), rotAngles)
        ])
        # print('---- Shifts ----')
        # print([ 'dxy{0} = ({1:.1f}, {2:.1f}) px\n'.format(idx + 1, sh[0], sh[1]) for idx, sh in zip(range(3), shifts) ])
        # print('------------------')
        # print('Average magnification = {0:.2f}x'.format(magAvg))
        print('Average rotation = {0:.2f} deg'.format(rotAngleAvg))
        # print('Average shift = ({0:.0f}, {1:.0f}) px'.format(shiftAvg[0], shiftAvg[1]))

        # img2Mag = tr.RescaleImageSki2(img2Rc, magAvg)
        img2Rot = tr.RotateImageSki2(img2Rc, rotAngleAvg, cut=False)
        # img2Rot = imsup.RotateImage(img2Rc, rotAngleAvg)
        padSz = (img2Rot.width - img1Rc.width) // 2
        img1RcPad = imsup.PadImage(img1Rc, padSz, 0.0, 'tblr')

        img1RcPad.MoveToCPU()
        img2Rot.MoveToCPU()
        img1RcPad.UpdateBuffer()
        img2Rot.UpdateBuffer()
        tmpImgList = imsup.ImageList([self.image, img1RcPad, img2Rot])
        tmpImgList.UpdateLinks()
        for img in tmpImgList:
            print(img.numInSeries)
        self.pointSets.append([])
        self.pointSets.append([])
        self.changePixmap(True)

        print('Triangulation complete!')
Exemple #2
0
    def triangulateAdvanced(self):
        triangles = [[
            CalcRealCoords(const.dimSize, self.pointSets[trIdx][pIdx])
            for pIdx in range(3)
        ] for trIdx in range(2)]
        # tr1 = [ CalcRealCoords(const.dimSize, self.pointSets[0][pIdx]) for pIdx in range(3) ]
        # tr2 = [ CalcRealCoords(const.dimSize, self.pointSets[1][pIdx]) for pIdx in range(3) ]
        # tr1 = self.pointSets[0][:3]
        # tr2 = self.pointSets[1][:3]

        tr1Dists = [
            CalcDistance(triangles[0][pIdx1], triangles[0][pIdx2])
            for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2])
        ]
        tr2Dists = [
            CalcDistance(triangles[1][pIdx1], triangles[1][pIdx2])
            for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2])
        ]

        # zrobic prostsza wersje oparta na zalozeniu ze rotCenter = [0, 0]
        # i bardziej zaawansowana, ktora bierze pod uwage inne polozenie srodka obrotu (rotCenter != [0, 0])
        # w tym drugim przypadku potrzebne jest obliczenie shiftow
        # mozna wyznaczyc dokladniej (sredni) rotCenter (na podstawie trzech a nie dwoch punktow)
        rcSum = [0, 0]
        rotCenters = []
        for idx1 in range(3):
            for idx2 in range(idx1 + 1, 3):
                print(idx1, idx2)
                rotCenter = tr.FindRotationCenter(
                    [triangles[0][idx1], triangles[0][idx2]],
                    [triangles[1][idx1], triangles[1][idx2]])
                rotCenters.append(rotCenter)
                print('rotCenter = {0}'.format(rotCenter))
                rcSum = list(np.array(rcSum) + np.array(rotCenter))

        rotCenterAvg = list(np.array(rcSum) / 3.0)
        rcShift = []
        rcShift[:] = rotCenters[0][:]
        rcShift.reverse()
        print('rotCenterAvg = {0}'.format(rotCenterAvg))

        # shift(-rotCenter) obu obrazow
        rcShift = [-int(rc) for rc in rcShift]
        print('rcShift = {0}'.format(rcShift))
        img1 = imsup.CopyImage(self.image.prev)
        img2 = imsup.CopyImage(self.image)
        imsup.SaveAmpImage(img1, 'img1.png')
        imsup.SaveAmpImage(img2, 'img2.png')
        img1Rc = cc.ShiftImage(img1, rcShift)
        img2Rc = cc.ShiftImage(img2, rcShift)
        cropCoords = imsup.MakeSquareCoords(
            imsup.DetermineCropCoords(img1Rc.width, img1Rc.height, rcShift))
        img1Rc = imsup.CropImageROICoords(img1Rc, cropCoords)
        img2Rc = imsup.CropImageROICoords(img2Rc, cropCoords)
        imsup.SaveAmpImage(img1Rc, 'holo1.png')
        imsup.SaveAmpImage(img2Rc, 'img2rc.png')

        rotAngles = []
        for idx, p1, p2 in zip(range(3), triangles[0], triangles[1]):
            p1New = CalcNewCoords(p1, rotCenters[0])
            p2New = CalcNewCoords(p2, rotCenters[0])
            triangles[0][idx] = p1New
            triangles[1][idx] = p2New
            rotAngles.append(CalcRotAngle(p1New, p2New))

        rotAngleAvg = np.average(rotAngles)

        mags = [dist1 / dist2 for dist1, dist2 in zip(tr1Dists, tr2Dists)]
        magAvg = np.average(mags)

        tr1InnerAngles = [
            CalcInnerAngle(a, b, c)
            for a, b, c in zip(tr1Dists, tr1Dists[-1:] +
                               tr1Dists[:-1], tr1Dists[-2:] + tr1Dists[:-2])
        ]
        tr2InnerAngles = [
            CalcInnerAngle(a, b, c)
            for a, b, c in zip(tr2Dists, tr2Dists[-1:] +
                               tr2Dists[:-1], tr2Dists[-2:] + tr2Dists[:-2])
        ]

        triangles[1] = [
            tr.RotatePoint(p, ang) for p, ang in zip(triangles[1], rotAngles)
        ]
        shifts = [
            list(np.array(p1) - np.array(p2))
            for p1, p2 in zip(triangles[0], triangles[1])
        ]
        shiftAvg = [
            np.average([sh[0] for sh in shifts]),
            np.average([sh[1] for sh in shifts])
        ]
        shiftAvg = [int(round(sh)) for sh in shiftAvg]

        print('---- Triangle 1 ----')
        print([
            'R{0} = {1:.2f} px\n'.format(idx + 1, dist)
            for idx, dist in zip(range(3), tr1Dists)
        ])
        print([
            'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle)
            for idx, angle in zip(range(3), tr1InnerAngles)
        ])
        # print('R12 = {0:.2f} px\nR13 = {1:.2f} px\nR23 = {2:.2f} px\n---'.format(r12, r13, r23))
        # print('a1 = {0:.0f} deg\na2 = {1:.0f} deg\na3 = {2:.0f} deg\n---'.format(alpha1, alpha2, alpha3))
        print('---- Triangle 2 ----')
        print([
            'R{0} = {1:.2f} px\n'.format(idx + 1, dist)
            for idx, dist in zip(range(3), tr2Dists)
        ])
        print([
            'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle)
            for idx, angle in zip(range(3), tr2InnerAngles)
        ])
        # print('R12 = {0:.2f} px\nR13 = {1:.2f} px\nR23 = {2:.2f} px\n---'.format(R12, R13, R23))
        # print('a1 = {0:.0f} deg\na2 = {1:.0f} deg\na3 = {2:.0f} deg\n---'.format(Alpha1, Alpha2, Alpha3))
        print('---- Magnification ----')
        print([
            'mag{0} = {1:.2f}x\n'.format(idx + 1, mag)
            for idx, mag in zip(range(3), mags)
        ])
        print('---- Rotation ----')
        print([
            'phi{0} = {1:.0f} deg\n'.format(idx + 1, angle)
            for idx, angle in zip(range(3), rotAngles)
        ])
        print('---- Shifts ----')
        print([
            'dxy{0} = ({1:.1f}, {2:.1f}) px\n'.format(idx + 1, sh[0], sh[1])
            for idx, sh in zip(range(3), shifts)
        ])
        print('------------------')
        print('Average magnification = {0:.2f}x'.format(magAvg))
        print('Average rotation = {0:.2f} deg'.format(rotAngleAvg))
        print('Average shift = ({0:.0f}, {1:.0f}) px'.format(
            shiftAvg[0], shiftAvg[1]))

        # img2Mag = tr.RescaleImageSki2(img2Rc, magAvg)
        # imsup.SaveAmpImage(img2Mag, 'img2_mag.png')
        img2Rot = tr.RotateImageSki2(img2Rc, rotAngleAvg, cut=False)
        imsup.SaveAmpImage(img2Rot, 'holo2.png')
        # cropCoords = imsup.DetermineCropCoordsForNewWidth(img1Rc.width, img2Rot.width)
        # img1Crop = imsup.CropImageROICoords(img1Rc, cropCoords)
        # imsup.SaveAmpImage(img1Crop, 'holo1.png')

        # ---

        # imgs1H = imsup.LinkTwoImagesSmoothlyH(img1Crop, img1Crop)
        # linkedImages1 = imsup.LinkTwoImagesSmoothlyV(imgs1H, imgs1H)
        # imgs2H = imsup.LinkTwoImagesSmoothlyH(img2Rot, img2Rot)
        # linkedImages2 = imsup.LinkTwoImagesSmoothlyV(imgs2H, imgs2H)
        #
        # img1Alg, img2Alg = cc.AlignTwoImages(linkedImages1, linkedImages2, [0, 1, 2])
        #
        # newCoords = imsup.DetermineCropCoords(img1Crop.width, img1Crop.height, img2Alg.shift)
        # newSquareCoords = imsup.MakeSquareCoords(newCoords)
        # print(newSquareCoords)
        # newSquareCoords[2:4] = list(np.array(newSquareCoords[2:4]) - np.array(newSquareCoords[:2]))
        # newSquareCoords[:2] = [0, 0]
        # print(newSquareCoords)
        #
        # img1Res = imsup.CropImageROICoords(img1Alg, newSquareCoords)
        # img2Res = imsup.CropImageROICoords(img2Alg, newSquareCoords)

        # imsup.SaveAmpImage(img1Alg, 'holo1_big.png')
        # imsup.SaveAmpImage(img2Alg, 'holo2_big.png')

        # imsup.SaveAmpImage(img1Res, 'holo1.png')
        # imsup.SaveAmpImage(img2Res, 'holo2.png')

        self.pointSets[0][:] = [
            CalcNewCoords(SwitchXY(rotCenters[idx]), [-512, -512])
            for idx in range(3)
        ]
        self.pointSets[1][:] = [
            CalcNewCoords(SwitchXY(rotCenters[idx]), [-512, -512])
            for idx in range(3)
        ]
        print(self.pointSets[0])
        print(self.pointSets[1])

        return