Deskew MNIST图像(Deskew MNIST images)
我在https://fsix.github.io/mnist/Deskewing.html上找到了如何校正MNIST数据集的图像。 它似乎工作。 我的问题是,在去偏移之前,每个像素的值都在0和1之间。但是在对图像进行校正后,值不再在0和1之间。 它们可以是负数,也可以大于1.如何解决?
这是代码:
def moments(image): c0,c1 = np.mgrid[:image.shape[0],:image.shape[1]] # A trick in numPy to create a mesh grid totalImage = np.sum(image) #sum of pixels m0 = np.sum(c0*image)/totalImage #mu_x m1 = np.sum(c1*image)/totalImage #mu_y m00 = np.sum((c0-m0)**2*image)/totalImage #var(x) m11 = np.sum((c1-m1)**2*image)/totalImage #var(y) m01 = np.sum((c0-m0)*(c1-m1)*image)/totalImage #covariance(x,y) mu_vector = np.array([m0,m1]) # Notice that these are \mu_x, \mu_y respectively covariance_matrix = np.array([[m00,m01],[m01,m11]]) # Do you see a similarity between the covariance matrix return mu_vector, covariance_matrix def deskew(image): c,v = moments(image) alpha = v[0,1]/v[0,0] affine = np.array([[1,0],[alpha,1]]) ocenter = np.array(image.shape)/2.0 offset = c-np.dot(affine,ocenter) return interpolation.affine_transform(image,affine,offset=offset)
I found on https://fsix.github.io/mnist/Deskewing.html how to deskew the images of the MNIST dataset. It seems to work. My problem is that before deskewing each pixel has a value between 0 and 1. But after deskewing the image the values are not between 0 and 1 any more. They can be negative and can be greater than 1. How can this be fixed?
Here is the code:
def moments(image): c0,c1 = np.mgrid[:image.shape[0],:image.shape[1]] # A trick in numPy to create a mesh grid totalImage = np.sum(image) #sum of pixels m0 = np.sum(c0*image)/totalImage #mu_x m1 = np.sum(c1*image)/totalImage #mu_y m00 = np.sum((c0-m0)**2*image)/totalImage #var(x) m11 = np.sum((c1-m1)**2*image)/totalImage #var(y) m01 = np.sum((c0-m0)*(c1-m1)*image)/totalImage #covariance(x,y) mu_vector = np.array([m0,m1]) # Notice that these are \mu_x, \mu_y respectively covariance_matrix = np.array([[m00,m01],[m01,m11]]) # Do you see a similarity between the covariance matrix return mu_vector, covariance_matrix def deskew(image): c,v = moments(image) alpha = v[0,1]/v[0,0] affine = np.array([[1,0],[alpha,1]]) ocenter = np.array(image.shape)/2.0 offset = c-np.dot(affine,ocenter) return interpolation.affine_transform(image,affine,offset=offset)
原文:https://stackoverflow.com/questions/43577665
最满意答案
您可以搜索在
end
之前的任何地方开始的任何项目,并在start
之后的任何地方结束(这与您使用nor
表示的方式类似,只是稍微简单一些):db.collection.find({ "start.date": { $lt: end }, "end.date": { $gt: start } });
You search items that start anywhere before
end
and end anywhere afterstart
(which is similar to how you express it with yournor
, only slightly simpler):db.collection.find({ "start.date": { $lt: end }, "end.date": { $gt: start } });
相关问答
更多-
按日期查询Java / MongoDB(Java/MongoDB query by date)[2023-01-26]
你所做的只是用{$lte: toDate}在密钥覆盖中丢失$gte操作符来查询。 你想要的是: query.put("dateAdded", BasicDBObjectBuilder.start("$gte", fromDate).add("$lte", toDate).get()); What you're doing is querying only with {$lte: toDate} losing $gte operator in the key overwrite. What you want ... -
尽管你的分页语法是错误的,但使用ObjectId()是完全正确的。 你要: db.tweets.find().limit(50).sort({"_id":-1}); 这就是说你想按照_id值排序的推文以降序排列,而你想要最近的50个。你的问题是,当前结果集正在改变时分页是棘手的 - 所以你不想在下一页使用跳过,记下结果集中最小的_id (最近的第50个_id值),然后用下面的代码获取下一页: db.tweets.find( {_id : { "$lt" : <50th _id> } } ).limit ...
-
您可以使用日期聚合运算符使用聚合框架来完成此操作 。 假设dtCreated是一个ISODate字段,你可以尝试这样的事情: query = [ { '$project': { 'year': {'$year':'$dtCreated'}, 'month': {'$month':'$dtCreated'}, 'day':{'$dayOfMonth':'$dtCreated'} } }, ...
-
最后我克服了一个解决方案,但我仍然不知道出了什么问题,我从2.1更新了monogDB版本到2.4.4,然后一切正常,因为我的预期 谢谢 Finally i overcome with a solution but still i don't know what was wrong, I updated monogDB version to 2.4.4 from 2.1, then everything works as i expected Thanks
-
如此处所述,operator $date是将JSON格式扩展为存储date对象。 运行查询时,可能无法在所有GUI工具中识别它。 相反,您可能希望使用ISODate("2016-04-01T00:00:00.000Z") : $match: { "account_balances_date": { "$gte": ISODate("2016-04-01T00:00:00.000Z"), "$lte": ISODate("2016-08-01T00:00:00.000Z ...
-
你的解决方案很接近。 db.DailyReports.aggregate([{ $project: { reports: { $filter: { input: '$reports', as: 'report', cond: { $and: [ { $gte: [ '$$report.startDate', new ISODate('2017-07-02') ] }, { ...
-
MongoDB - 查询酒店日期范围的可用日期范围(MongoDB - Query Available Date Range with a Date Range for Hotel)[2021-10-28]
我实际上希望你的文档有真正的 ISOD,而不是看起来像字符串。 当他们这样做时,以下查询表单按预期匹配: db.collection.find({ "available": { "$elemMatch": { "start": { "$gte": new Date("2014-04-17") }, "end": { "$gte": new Date("2014-04-21") } } } }) Wel ... -
您可以搜索在end之前的任何地方开始的任何项目,并在start之后的任何地方结束(这与您使用nor表示的方式类似,只是稍微简单一些): db.collection.find({ "start.date": { $lt: end }, "end.date": { $gt: start } }); You search items that start anywhere before end and end anywhere after start (which is similar to h ...
-
您可以使用带有$month投影运算符的aggregate来执行此操作: db.customer.aggregate([ {$project: {name: 1, month: {$month: '$bday'}}}, {$match: {month: 9}} ]); You can do that using aggregate with the $month projection operator: db.customer.aggregate([ {$project: {name: 1, m ...
-
在没有日期时间的特定月份或范围月份上查询Mongodb(Query Mongodb on specific or range month without year of a datetime)[2021-09-28]
$month是一个聚合运算符,因此它只能与aggregate一起使用,而不能find : db.test.aggregate([ {$project: { id: 1, date: 1, month: {$month: '$date'} }}, {$match: {month: 11}}, {$project: { id: 1, date: 1 }} ]) 结果: { "resu ...