How to speed up the retrieval of the corners of a maskOverlapping rectanglesHow to speed up Python web...

Compare four integers, return word based on maximum

Exponential growth/decay formula: what happened to the other constant of integration?

How would we write a misogynistic character without offending people?

What to do when being responsible for data protection in your lab, yet advice is ignored?

What is a term for a function that when called repeatedly, has the same effect as calling once?

Can you use a beast's innate abilities while polymorphed?

How do ISS astronauts "get their stripes"?

Most significant research articles for practical investors with research perspectives

Reason Why Dimensional Travelling Would be Restricted

How to mitigate "bandwagon attacking" from players?

Is my plan for fixing my water heater leak bad?

Use comma instead of & in table

How to count occurrences of Friday 13th

What is better: yes / no radio, or simple checkbox?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Logistics of a hovering watercraft in a fantasy setting

How do I construct an nxn matrix?

Understanding Kramnik's play in game 1 of Candidates 2018

How can atoms be electrically neutral when there is a difference in the positions of the charges?

How to deny access to SQL Server to certain login over SSMS, but allow over .Net SqlClient Data Provider

You'll find me clean when something is full

What is the wife of a henpecked husband called?

Make me a metasequence

How to count words in a line



How to speed up the retrieval of the corners of a mask


Overlapping rectanglesHow to speed up Python web crawler?Iterate over million points to get unique closest pointBFS shortest path for Google Foobar “Prepare the Bunnies' Escape”Backpropagation in simple Neural NetworkBFS shortest path for Google Foobar challenge “Prepare the Bunnies' Escape”Shortest Path For Google Foobar (Prepare The Bunnies Escape)Shortest path challengeMax Increase to Keep City Skyline in JavaGoogle FooBar “Prepare The Bunnies Escape”













0












$begingroup$


I have trained a polygon detector neural network to recognize the mask of "quadrilateral" (the mask generates curvy lines so it's not exactly a quadrilateral). I would like to get the corners of the quadrilateral.



I believe the best approach is to get the points in the mask that are closest to the corners of the image. First question is are these valid assumptions? Second question is is this the best approach?




Top-Left is minimum distance between (0,0) and mask.



Top-Right is minimum distance between (width, 0) and mask.



Bottom-Left is minimum distance between (0, height) and mask.



Bottom-Right is minimum distance between (width, height) and mask.




The last question is my implementation is slow. The Neural network generates the mask in .7 seconds, but it's taking my loop ~2 seconds to find the corners. Can this be sped up?



def predict(self,img):
# Read image
image = img
height,width,channels=img.shape

# Detect objects
r = self.model.detect([image], verbose=0)[0]
mask=r['masks']
print(mask)
x1=0
x2=0
x3=0
x4=0
y1=0
y2=0
y3=0
y4=0
minDistanceTopLeft=999999
minDistanceTopRight=999999
minDistanceBottomLeft=999999
minDistanceBottomRight=999999
xAverage=0.0
yAverage=0.0
for x in range(0, len(mask)):
for y in range(0, len(mask[x])):
if(mask[x][y]):
distToTopLeft=(x-0)*(x-0)+(y-0)*(y-0)
if(distToTopLeft<minDistanceTopLeft):
minDistanceTopLeft=distToTopLeft
x1=x
y1=y
distToTopRight=(x-width)*(x-width)+(y-0)*(y-0)
if(distToTopRight<minDistanceTopRight):
minDistanceTopRight=distToTopRight
x2=x
y2=y
distToBottomLeft=(x-0)*(x-0)+(y-height)*(y-height)
if(distToBottomLeft<minDistanceBottomLeft):
minDistanceBottomLeft=distToBottomLeft
x4=x
y4=y
distToBottomRight=(x-width)*(x-width)+(y-height)*(y-height)
if(distToBottomRight<minDistanceBottomRight):
minDistanceBottomRight=distToBottomRight
x3=x
y3=y
toReturn=np.array([x1, y1, x2, y2, x3, y3, x4, y4, 1])
return [toReturn.tolist()]


Mask is a numpy array of booleans (ie)



[[False]
[False]
[False]
...
[False]
[False]
[False]]








share











$endgroup$












  • $begingroup$
    Oversight on my part- thanks @200_success have updated
    $endgroup$
    – Seth Kitchen
    49 secs ago
















0












$begingroup$


I have trained a polygon detector neural network to recognize the mask of "quadrilateral" (the mask generates curvy lines so it's not exactly a quadrilateral). I would like to get the corners of the quadrilateral.



I believe the best approach is to get the points in the mask that are closest to the corners of the image. First question is are these valid assumptions? Second question is is this the best approach?




Top-Left is minimum distance between (0,0) and mask.



Top-Right is minimum distance between (width, 0) and mask.



Bottom-Left is minimum distance between (0, height) and mask.



Bottom-Right is minimum distance between (width, height) and mask.




The last question is my implementation is slow. The Neural network generates the mask in .7 seconds, but it's taking my loop ~2 seconds to find the corners. Can this be sped up?



def predict(self,img):
# Read image
image = img
height,width,channels=img.shape

# Detect objects
r = self.model.detect([image], verbose=0)[0]
mask=r['masks']
print(mask)
x1=0
x2=0
x3=0
x4=0
y1=0
y2=0
y3=0
y4=0
minDistanceTopLeft=999999
minDistanceTopRight=999999
minDistanceBottomLeft=999999
minDistanceBottomRight=999999
xAverage=0.0
yAverage=0.0
for x in range(0, len(mask)):
for y in range(0, len(mask[x])):
if(mask[x][y]):
distToTopLeft=(x-0)*(x-0)+(y-0)*(y-0)
if(distToTopLeft<minDistanceTopLeft):
minDistanceTopLeft=distToTopLeft
x1=x
y1=y
distToTopRight=(x-width)*(x-width)+(y-0)*(y-0)
if(distToTopRight<minDistanceTopRight):
minDistanceTopRight=distToTopRight
x2=x
y2=y
distToBottomLeft=(x-0)*(x-0)+(y-height)*(y-height)
if(distToBottomLeft<minDistanceBottomLeft):
minDistanceBottomLeft=distToBottomLeft
x4=x
y4=y
distToBottomRight=(x-width)*(x-width)+(y-height)*(y-height)
if(distToBottomRight<minDistanceBottomRight):
minDistanceBottomRight=distToBottomRight
x3=x
y3=y
toReturn=np.array([x1, y1, x2, y2, x3, y3, x4, y4, 1])
return [toReturn.tolist()]


Mask is a numpy array of booleans (ie)



[[False]
[False]
[False]
...
[False]
[False]
[False]]








share











$endgroup$












  • $begingroup$
    Oversight on my part- thanks @200_success have updated
    $endgroup$
    – Seth Kitchen
    49 secs ago














0












0








0





$begingroup$


I have trained a polygon detector neural network to recognize the mask of "quadrilateral" (the mask generates curvy lines so it's not exactly a quadrilateral). I would like to get the corners of the quadrilateral.



I believe the best approach is to get the points in the mask that are closest to the corners of the image. First question is are these valid assumptions? Second question is is this the best approach?




Top-Left is minimum distance between (0,0) and mask.



Top-Right is minimum distance between (width, 0) and mask.



Bottom-Left is minimum distance between (0, height) and mask.



Bottom-Right is minimum distance between (width, height) and mask.




The last question is my implementation is slow. The Neural network generates the mask in .7 seconds, but it's taking my loop ~2 seconds to find the corners. Can this be sped up?



def predict(self,img):
# Read image
image = img
height,width,channels=img.shape

# Detect objects
r = self.model.detect([image], verbose=0)[0]
mask=r['masks']
print(mask)
x1=0
x2=0
x3=0
x4=0
y1=0
y2=0
y3=0
y4=0
minDistanceTopLeft=999999
minDistanceTopRight=999999
minDistanceBottomLeft=999999
minDistanceBottomRight=999999
xAverage=0.0
yAverage=0.0
for x in range(0, len(mask)):
for y in range(0, len(mask[x])):
if(mask[x][y]):
distToTopLeft=(x-0)*(x-0)+(y-0)*(y-0)
if(distToTopLeft<minDistanceTopLeft):
minDistanceTopLeft=distToTopLeft
x1=x
y1=y
distToTopRight=(x-width)*(x-width)+(y-0)*(y-0)
if(distToTopRight<minDistanceTopRight):
minDistanceTopRight=distToTopRight
x2=x
y2=y
distToBottomLeft=(x-0)*(x-0)+(y-height)*(y-height)
if(distToBottomLeft<minDistanceBottomLeft):
minDistanceBottomLeft=distToBottomLeft
x4=x
y4=y
distToBottomRight=(x-width)*(x-width)+(y-height)*(y-height)
if(distToBottomRight<minDistanceBottomRight):
minDistanceBottomRight=distToBottomRight
x3=x
y3=y
toReturn=np.array([x1, y1, x2, y2, x3, y3, x4, y4, 1])
return [toReturn.tolist()]


Mask is a numpy array of booleans (ie)



[[False]
[False]
[False]
...
[False]
[False]
[False]]








share











$endgroup$




I have trained a polygon detector neural network to recognize the mask of "quadrilateral" (the mask generates curvy lines so it's not exactly a quadrilateral). I would like to get the corners of the quadrilateral.



I believe the best approach is to get the points in the mask that are closest to the corners of the image. First question is are these valid assumptions? Second question is is this the best approach?




Top-Left is minimum distance between (0,0) and mask.



Top-Right is minimum distance between (width, 0) and mask.



Bottom-Left is minimum distance between (0, height) and mask.



Bottom-Right is minimum distance between (width, height) and mask.




The last question is my implementation is slow. The Neural network generates the mask in .7 seconds, but it's taking my loop ~2 seconds to find the corners. Can this be sped up?



def predict(self,img):
# Read image
image = img
height,width,channels=img.shape

# Detect objects
r = self.model.detect([image], verbose=0)[0]
mask=r['masks']
print(mask)
x1=0
x2=0
x3=0
x4=0
y1=0
y2=0
y3=0
y4=0
minDistanceTopLeft=999999
minDistanceTopRight=999999
minDistanceBottomLeft=999999
minDistanceBottomRight=999999
xAverage=0.0
yAverage=0.0
for x in range(0, len(mask)):
for y in range(0, len(mask[x])):
if(mask[x][y]):
distToTopLeft=(x-0)*(x-0)+(y-0)*(y-0)
if(distToTopLeft<minDistanceTopLeft):
minDistanceTopLeft=distToTopLeft
x1=x
y1=y
distToTopRight=(x-width)*(x-width)+(y-0)*(y-0)
if(distToTopRight<minDistanceTopRight):
minDistanceTopRight=distToTopRight
x2=x
y2=y
distToBottomLeft=(x-0)*(x-0)+(y-height)*(y-height)
if(distToBottomLeft<minDistanceBottomLeft):
minDistanceBottomLeft=distToBottomLeft
x4=x
y4=y
distToBottomRight=(x-width)*(x-width)+(y-height)*(y-height)
if(distToBottomRight<minDistanceBottomRight):
minDistanceBottomRight=distToBottomRight
x3=x
y3=y
toReturn=np.array([x1, y1, x2, y2, x3, y3, x4, y4, 1])
return [toReturn.tolist()]


Mask is a numpy array of booleans (ie)



[[False]
[False]
[False]
...
[False]
[False]
[False]]






python performance





share














share












share



share








edited 1 min ago







Seth Kitchen

















asked 7 mins ago









Seth KitchenSeth Kitchen

1937




1937












  • $begingroup$
    Oversight on my part- thanks @200_success have updated
    $endgroup$
    – Seth Kitchen
    49 secs ago


















  • $begingroup$
    Oversight on my part- thanks @200_success have updated
    $endgroup$
    – Seth Kitchen
    49 secs ago
















$begingroup$
Oversight on my part- thanks @200_success have updated
$endgroup$
– Seth Kitchen
49 secs ago




$begingroup$
Oversight on my part- thanks @200_success have updated
$endgroup$
– Seth Kitchen
49 secs ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214738%2fhow-to-speed-up-the-retrieval-of-the-corners-of-a-mask%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214738%2fhow-to-speed-up-the-retrieval-of-the-corners-of-a-mask%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...