创建距曲线的距离矩阵

时间:2019-02-20 02:38:31

标签: python image numpy scipy

我正在尝试创建一个矩阵,其元素是到我定义的曲线的距离(下面的代码):

spiral curve in a matrix

我想对这张图像进行一些处理,使我得到一个矩阵,其中包含该点和螺旋线上任意点之间的所有最小欧几里得距离。

我尝试像这样使用scipy的{​​{1}}:

ndimage.distance_transform_edt

但是输出并不能满足我的需求!

有人知道如何生成此矩阵吗?

下面的螺旋线生成代码:

import scipy.ndimage as ndi
transformed = ndi.distance_transform_edt(spiral())

1 个答案:

答案 0 :(得分:2)

根据import scipy.ndimage as ndi # The original spiral curve, with 1's where the curve is defined, else 0 s = spiral() # Transformed data: 0's representing the curve, with 1's everywhere else TS= 1-s transformed = ndi.distance_transform_edt(TS) 上的three-value logic,该函数将计算非零矩阵的元素到零元素的最近欧几里得距离。

问题在于您的function addBorders() { //Access the sheet which contains all the IDs var idSheet = SpreadsheetApp.openById('example'); //Check for the last row var lastRow = 0; var currRow = 2; //Loop through the sheet and find the last ID available while (idSheet.getRange("A" + currRow).getValue() != "") { currRow = currRow + 1; } //Set the last row with an ID found to be the lastRow lastRow = currRow; idSheet.getRange("B1").setValue("Total Rows"); idSheet.getRange("B2").setValue(lastRow); //Loop through the ID sheet to find the ID to make changes to for( var y = 2; y < lastRow; y++) { var studentID = idSheet.getSheetByName("Sheet1").getRange(y, 1).getValue(); //As IDs are take from the ID sheet, open the relevant sheet and run the code below var ss = SpreadsheetApp.openById(studentID); var sheetsCount = ss.getNumSheets(); var sheets = ss.getSheets(); //For each sheet in the individal student spreadsheets, set the borders correctly for (var i = 0; i < sheetsCount; i++) { var sheet = sheets[i]; var range = sheet.getRange(6, 3, 35); var values = range.getValues().map(function(d){ return d[0] }); //clear previous border var selection = sheet.getRange(6,2,35,5) selection.setBorder(false,false,false,false,false,false); //set border var index = values.indexOf(""); var border = sheet.getRange(5, 2, index+1, 5); border.setBorder(true, true, true, true, true, true); } } } 函数返回的曲线为非零(精确等于1),其中存在曲线,而其他任何地方为0。要解决此问题:

{{1}}

这些努力导致了以下情节:

this stackoverflow discussion

相关问题