0

我有一些与通过旋转移动粒子(强度为高斯分布的圆形粒子)有关的问题。当我旋转图像时,我希望当粒子最终位于子像素位置时,粒子强度能够正确重新分布。假设我在 2,2 处有一个粒子,我将它旋转了大约 4,3(注意:不是图像的几何中心)12 度,它最终在 3.2,4.6 处。

我试图挖掘一些东西,我发现了这个这个

我不是很相信。我在这里附上我用这段代码合成的图片

#from random import randrange, uniform
import math
#import matplotlib as mpl
#import matplotlib.pyplot as plt
#import trackpy as tp
#import pims
import numpy as np
from PIL import Image

""" Class to specify the location of a particle """

class Particles(object):

    def __init__(self,n=200):

        self.pointsxy = np.zeros((200,2))

#""" Initial location of particles. We use floating point ( not integer) to specify random location """       

    def randomloc(self) :  

        for i in range(1,200) :
            self.pointsxy[i][0] = np.random.uniform(0,1280)
            self.pointsxy[i][1] = np.random.uniform(0,1024)
            frame.add_spot((self.pointsxy[i][0], self.pointsxy[i][1]), 200, 5)


        return self.pointsxy
#""" Function to displace or translate the particles from initial location to the specified value """

    def displacement(self,xdisp=0,ydisp=10,n=100):

        newxy = np.zeros((200,2))  

        for i in range(1,200) :   
            newxy[i][0]=self.pointsxy[i][0]+xdisp
            newxy[i][1]=self.pointsxy[i][1]+ydisp
            frame2.add_spot((newxy[i][0], newxy[i][1]), 200, 5)

        return newxy

#"""function to rotate the image by a specified angle in degrees as provided in main function """    
    def rotate(self, origin,angle, n=10):
        """
        Rotate a point counterclockwise by a given angle around a given origin.

       note : The angle should be given in radians.
        """
        ox, oy = origin

        xynew = np.zeros((200,2))

        for i in range(1,200) :
            px = self.pointsxy[i][0]
            py = self.pointsxy[i][1]

            xynew[i][0] = ox + math.cos(angle) * (self.pointsxy[i][0] - ox) - math.sin(angle) * (self.pointsxy[i][1] - oy)
            xynew[i][1] = oy + math.sin(angle) * (self.pointsxy[i][0] - ox) + math.cos(angle) * (self.pointsxy[i][1] - oy)


            frame3.add_spot((xynew[i][0],xynew[i][1]), 255, 5)
        return xynew

#   def make_image(self):
#      
#        for i in range(1,100) :
#            frame.add_spot((pointsxy[i][0], pointsxy[i][1]), 200, 5)

#""" class to create image of particles in the specified location as per the class MakeParticles"""
class SimulatedFrame(object):

    def __init__(self, shape, dtype=np.int8):
        self.image = np.zeros(shape, dtype=dtype)
        self._saturation = np.iinfo(dtype).max
        self.shape = shape
        self.dtype =dtype
#""" A gaussian distribution of intensities is obtained. Eccentricity (ecc) means how much the particle is elongated . """

    def add_spot(self, pos, amplitude, r, ecc=0):
        "Add a Gaussian spot to the frame."
        x, y = np.meshgrid(*np.array(list(map(np.arange, self.shape))) - np.asarray(pos))
        spot = amplitude*np.exp(-((x/(1 - ecc))**2 + (y*(1 - ecc))**2)/(2*r**2)).T
        self.image += np.clip(spot, 0, self._saturation).astype(self.dtype)

    def save_frame(self, filename='frame.jpg'):
        img = Image.fromarray(self.image.astype(np.uint8))
        img.save(filename)

#class MakeImage(object):
#    def blob(self,array):

#""" the values in brackets say the size of the background to be created. Use np.int so that the particle overlap does not cause regions of black spots """       
frame = SimulatedFrame((1280, 1024), dtype=np.int)
frame2 = SimulatedFrame((1280, 1024), dtype=np.int)
frame3 = SimulatedFrame((1280, 1024), dtype=np.int)

coordinates=Particles()
xy=coordinates.randomloc()
nee=coordinates.displacement()
origin=640,512
new=coordinates.rotate(origin,angle=math.radians(5))

fname = "initial_image.jpg"
frame.save_frame(filename=fname) 


fname1 = "translated_image.jpg"
frame2.save_frame(filename=fname1)  

fname3 = "rotat                     ed_image.jpg"
frame3.save_frame(filename=fname3) 

现在,我为每个新帧(平移、旋转等)创建新粒子,但我想操作初始图像。

谢谢阿伦

4

1 回答 1

0

我使用了以下代码,它可以工作。

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('1.jpg',0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),-40,1) # the format is cv2.getRotationMatrix2D(center, angle, scale) 
dst = cv2.warpAffine(img,M,(cols,rows),flags=cv2.INTER_CUBIC)
plt.imshow(dst)
于 2017-09-12T12:14:44.283 回答