how to crop the blank part from an image (document form) in python?2019 Community Moderator ElectionHow do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to lowercase a string in Python?Why is reading lines from stdin much slower in C++ than Python?How to remove a key from a Python dictionary?

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

An Undercover Army

Why are special aircraft used for the carriers in the United States Navy?

How to make sure I'm assertive enough in contact with subordinates?

Infinitive vs Gerund

Align equations with text before one of them

Paper published similar to PhD thesis

Problems with rounding giving too many digits

I can't die. Who am I?

Is this nominative case or accusative case?

What is the purpose of a disclaimer like "this is not legal advice"?

Why is there an extra space when I type "ls" on the Desktop?

Why aren't there more gauls like Obelix?

Ultrafilters as a double dual

Error in TransformedField

Why do phishing e-mails use faked e-mail addresses instead of the real one?

PTIJ: Aliyot for the deceased

The (Easy) Road to Code

School performs periodic password audits. Is my password compromised?

How can I be pwned if I'm not registered on the compromised site?

Is there a way to find out the age of climbing ropes?

Did Amazon pay $0 in taxes last year?

Convert an array of objects to array of the objects' values

PTiJ: How should animals pray?



how to crop the blank part from an image (document form) in python?



2019 Community Moderator ElectionHow do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to lowercase a string in Python?Why is reading lines from stdin much slower in C++ than Python?How to remove a key from a Python dictionary?










-2















enter image description hereI have a scanned copy of a document as an image submitted by the user, it covers only 40% of the paper's height. I want to crop only that part, how to achieve this.
It is not necessary that the required form will always be on the top of the paper, it can be anywhere and the rest is blank white paper, how to crop that part?



The scanned copy I have got using scanner made in python only, so it has little black dots in the page.










share|improve this question









New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Good starting point for an answer would be some testing material and your current approach

    – user8408080
    yesterday











  • I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

    – Karan Kanwal
    yesterday











  • Can you share image sample and expected result?

    – Alderven
    yesterday











  • I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

    – Karan Kanwal
    yesterday











  • Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

    – HansHirse
    yesterday















-2















enter image description hereI have a scanned copy of a document as an image submitted by the user, it covers only 40% of the paper's height. I want to crop only that part, how to achieve this.
It is not necessary that the required form will always be on the top of the paper, it can be anywhere and the rest is blank white paper, how to crop that part?



The scanned copy I have got using scanner made in python only, so it has little black dots in the page.










share|improve this question









New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Good starting point for an answer would be some testing material and your current approach

    – user8408080
    yesterday











  • I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

    – Karan Kanwal
    yesterday











  • Can you share image sample and expected result?

    – Alderven
    yesterday











  • I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

    – Karan Kanwal
    yesterday











  • Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

    – HansHirse
    yesterday













-2












-2








-2








enter image description hereI have a scanned copy of a document as an image submitted by the user, it covers only 40% of the paper's height. I want to crop only that part, how to achieve this.
It is not necessary that the required form will always be on the top of the paper, it can be anywhere and the rest is blank white paper, how to crop that part?



The scanned copy I have got using scanner made in python only, so it has little black dots in the page.










share|improve this question









New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












enter image description hereI have a scanned copy of a document as an image submitted by the user, it covers only 40% of the paper's height. I want to crop only that part, how to achieve this.
It is not necessary that the required form will always be on the top of the paper, it can be anywhere and the rest is blank white paper, how to crop that part?



The scanned copy I have got using scanner made in python only, so it has little black dots in the page.







python opencv python-imaging-library






share|improve this question









New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday







Karan Kanwal













New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Karan KanwalKaran Kanwal

44




44




New contributor




Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Karan Kanwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Good starting point for an answer would be some testing material and your current approach

    – user8408080
    yesterday











  • I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

    – Karan Kanwal
    yesterday











  • Can you share image sample and expected result?

    – Alderven
    yesterday











  • I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

    – Karan Kanwal
    yesterday











  • Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

    – HansHirse
    yesterday

















  • Good starting point for an answer would be some testing material and your current approach

    – user8408080
    yesterday











  • I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

    – Karan Kanwal
    yesterday











  • Can you share image sample and expected result?

    – Alderven
    yesterday











  • I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

    – Karan Kanwal
    yesterday











  • Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

    – HansHirse
    yesterday
















Good starting point for an answer would be some testing material and your current approach

– user8408080
yesterday





Good starting point for an answer would be some testing material and your current approach

– user8408080
yesterday













I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

– Karan Kanwal
yesterday





I have tried searching for similar image but could not found and I can't submit original document here so. It is just with less than half the height of a4 paper but user first got that document printed and sent that a4 paper as scanned copy, I need to crop that form only.

– Karan Kanwal
yesterday













Can you share image sample and expected result?

– Alderven
yesterday





Can you share image sample and expected result?

– Alderven
yesterday













I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

– Karan Kanwal
yesterday





I have share an image, I need the content part only, like you see 70% of portion is blank. But it won't be necessary that the content party will be on top always.

– Karan Kanwal
yesterday













Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

– HansHirse
yesterday





Use threshold, so that the (more or less) white part is neglected (use THRESH_BINARY_INV parameter). Then, just iterate over the image looking for the minimum and maximum x and y values with value 255. These four points describe a rectangle, where anything non-white should be present.

– HansHirse
yesterday












1 Answer
1






active

oldest

votes


















0














You can consider the steps below to crop the blank or the none blank part:



cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(xbNQF.png)"); // read the image

// main code starts from here
cv::Mat gray; // convert the image to gray and put the result in gray mat
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); // img -> gray
// threshold the gray image to remove the noise and put the result again in gray image
// it will convert all the background to black and all the text and fields to white
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// now enlage the text or the inpout text fields
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3)));
// now clean the image, remove unwanted small pixels
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// find all non zero to get the max y
cv::Mat idx; // the findNonZero() function will put the result in this mat
cv::findNonZero(gray, idx); // pass the mat idx to the function
// now iterate throgh the idx to find the max y
double maxY = 0; // this will keep the max y, init value is 0
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.y > maxY) // if this Y is greater than the last Y, copy it to the last Y
maxY = pnt.y; // this



// crop the none blank (upper) part
// NOTE: from this point you can also crop the blank part
// (0,0) means start form left-top, (gray.cols, int(maxY+5)) means
// whidth the same as the original image, and the height is
// the maxY + 5, 5 here means let give some margin the cropped image
// if you don't want, then you can delete it.
cv::Mat submat = img(cv::Rect(0, 0, gray.cols, int(maxY+5)));
cv::imshow("result", submat);

cv::waitKey();


And it is the result:



enter image description here



Hope it helps!



Update:
If you interest to all min, max of the (x, y), then search like this:



double maxX = 0, minX = std::numeric_limits<double>::max();
double maxY = 0, minY = std::numeric_limits<double>::max();
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.x > maxX)
maxX = pnt.x;

if (pnt.x < minX)
minX = pnt.x;

if (pnt.y > maxY)
maxY = pnt.y;

if (pnt.y < minY)
minY = pnt.y;




So then you can crop anywhere of the image once you have these points.






share|improve this answer

























  • This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

    – HansHirse
    yesterday











  • @HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

    – Bahramdun Adil
    yesterday












  • @KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

    – Bahramdun Adil
    yesterday











  • ok sure, I will try this. right now I am just writing the code by looking at your code.

    – Karan Kanwal
    yesterday











  • @KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

    – Bahramdun Adil
    yesterday










Your Answer






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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);



);






Karan Kanwal is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023295%2fhow-to-crop-the-blank-part-from-an-image-document-form-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can consider the steps below to crop the blank or the none blank part:



cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(xbNQF.png)"); // read the image

// main code starts from here
cv::Mat gray; // convert the image to gray and put the result in gray mat
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); // img -> gray
// threshold the gray image to remove the noise and put the result again in gray image
// it will convert all the background to black and all the text and fields to white
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// now enlage the text or the inpout text fields
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3)));
// now clean the image, remove unwanted small pixels
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// find all non zero to get the max y
cv::Mat idx; // the findNonZero() function will put the result in this mat
cv::findNonZero(gray, idx); // pass the mat idx to the function
// now iterate throgh the idx to find the max y
double maxY = 0; // this will keep the max y, init value is 0
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.y > maxY) // if this Y is greater than the last Y, copy it to the last Y
maxY = pnt.y; // this



// crop the none blank (upper) part
// NOTE: from this point you can also crop the blank part
// (0,0) means start form left-top, (gray.cols, int(maxY+5)) means
// whidth the same as the original image, and the height is
// the maxY + 5, 5 here means let give some margin the cropped image
// if you don't want, then you can delete it.
cv::Mat submat = img(cv::Rect(0, 0, gray.cols, int(maxY+5)));
cv::imshow("result", submat);

cv::waitKey();


And it is the result:



enter image description here



Hope it helps!



Update:
If you interest to all min, max of the (x, y), then search like this:



double maxX = 0, minX = std::numeric_limits<double>::max();
double maxY = 0, minY = std::numeric_limits<double>::max();
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.x > maxX)
maxX = pnt.x;

if (pnt.x < minX)
minX = pnt.x;

if (pnt.y > maxY)
maxY = pnt.y;

if (pnt.y < minY)
minY = pnt.y;




So then you can crop anywhere of the image once you have these points.






share|improve this answer

























  • This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

    – HansHirse
    yesterday











  • @HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

    – Bahramdun Adil
    yesterday












  • @KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

    – Bahramdun Adil
    yesterday











  • ok sure, I will try this. right now I am just writing the code by looking at your code.

    – Karan Kanwal
    yesterday











  • @KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

    – Bahramdun Adil
    yesterday















0














You can consider the steps below to crop the blank or the none blank part:



cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(xbNQF.png)"); // read the image

// main code starts from here
cv::Mat gray; // convert the image to gray and put the result in gray mat
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); // img -> gray
// threshold the gray image to remove the noise and put the result again in gray image
// it will convert all the background to black and all the text and fields to white
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// now enlage the text or the inpout text fields
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3)));
// now clean the image, remove unwanted small pixels
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// find all non zero to get the max y
cv::Mat idx; // the findNonZero() function will put the result in this mat
cv::findNonZero(gray, idx); // pass the mat idx to the function
// now iterate throgh the idx to find the max y
double maxY = 0; // this will keep the max y, init value is 0
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.y > maxY) // if this Y is greater than the last Y, copy it to the last Y
maxY = pnt.y; // this



// crop the none blank (upper) part
// NOTE: from this point you can also crop the blank part
// (0,0) means start form left-top, (gray.cols, int(maxY+5)) means
// whidth the same as the original image, and the height is
// the maxY + 5, 5 here means let give some margin the cropped image
// if you don't want, then you can delete it.
cv::Mat submat = img(cv::Rect(0, 0, gray.cols, int(maxY+5)));
cv::imshow("result", submat);

cv::waitKey();


And it is the result:



enter image description here



Hope it helps!



Update:
If you interest to all min, max of the (x, y), then search like this:



double maxX = 0, minX = std::numeric_limits<double>::max();
double maxY = 0, minY = std::numeric_limits<double>::max();
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.x > maxX)
maxX = pnt.x;

if (pnt.x < minX)
minX = pnt.x;

if (pnt.y > maxY)
maxY = pnt.y;

if (pnt.y < minY)
minY = pnt.y;




So then you can crop anywhere of the image once you have these points.






share|improve this answer

























  • This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

    – HansHirse
    yesterday











  • @HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

    – Bahramdun Adil
    yesterday












  • @KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

    – Bahramdun Adil
    yesterday











  • ok sure, I will try this. right now I am just writing the code by looking at your code.

    – Karan Kanwal
    yesterday











  • @KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

    – Bahramdun Adil
    yesterday













0












0








0







You can consider the steps below to crop the blank or the none blank part:



cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(xbNQF.png)"); // read the image

// main code starts from here
cv::Mat gray; // convert the image to gray and put the result in gray mat
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); // img -> gray
// threshold the gray image to remove the noise and put the result again in gray image
// it will convert all the background to black and all the text and fields to white
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// now enlage the text or the inpout text fields
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3)));
// now clean the image, remove unwanted small pixels
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// find all non zero to get the max y
cv::Mat idx; // the findNonZero() function will put the result in this mat
cv::findNonZero(gray, idx); // pass the mat idx to the function
// now iterate throgh the idx to find the max y
double maxY = 0; // this will keep the max y, init value is 0
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.y > maxY) // if this Y is greater than the last Y, copy it to the last Y
maxY = pnt.y; // this



// crop the none blank (upper) part
// NOTE: from this point you can also crop the blank part
// (0,0) means start form left-top, (gray.cols, int(maxY+5)) means
// whidth the same as the original image, and the height is
// the maxY + 5, 5 here means let give some margin the cropped image
// if you don't want, then you can delete it.
cv::Mat submat = img(cv::Rect(0, 0, gray.cols, int(maxY+5)));
cv::imshow("result", submat);

cv::waitKey();


And it is the result:



enter image description here



Hope it helps!



Update:
If you interest to all min, max of the (x, y), then search like this:



double maxX = 0, minX = std::numeric_limits<double>::max();
double maxY = 0, minY = std::numeric_limits<double>::max();
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.x > maxX)
maxX = pnt.x;

if (pnt.x < minX)
minX = pnt.x;

if (pnt.y > maxY)
maxY = pnt.y;

if (pnt.y < minY)
minY = pnt.y;




So then you can crop anywhere of the image once you have these points.






share|improve this answer















You can consider the steps below to crop the blank or the none blank part:



cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(xbNQF.png)"); // read the image

// main code starts from here
cv::Mat gray; // convert the image to gray and put the result in gray mat
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); // img -> gray
// threshold the gray image to remove the noise and put the result again in gray image
// it will convert all the background to black and all the text and fields to white
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// now enlage the text or the inpout text fields
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3)));
// now clean the image, remove unwanted small pixels
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// find all non zero to get the max y
cv::Mat idx; // the findNonZero() function will put the result in this mat
cv::findNonZero(gray, idx); // pass the mat idx to the function
// now iterate throgh the idx to find the max y
double maxY = 0; // this will keep the max y, init value is 0
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.y > maxY) // if this Y is greater than the last Y, copy it to the last Y
maxY = pnt.y; // this



// crop the none blank (upper) part
// NOTE: from this point you can also crop the blank part
// (0,0) means start form left-top, (gray.cols, int(maxY+5)) means
// whidth the same as the original image, and the height is
// the maxY + 5, 5 here means let give some margin the cropped image
// if you don't want, then you can delete it.
cv::Mat submat = img(cv::Rect(0, 0, gray.cols, int(maxY+5)));
cv::imshow("result", submat);

cv::waitKey();


And it is the result:



enter image description here



Hope it helps!



Update:
If you interest to all min, max of the (x, y), then search like this:



double maxX = 0, minX = std::numeric_limits<double>::max();
double maxY = 0, minY = std::numeric_limits<double>::max();
for (int i=0; i<idx.rows; ++i)
cv::Point pnt = idx.at<cv::Point>(i);
if (pnt.x > maxX)
maxX = pnt.x;

if (pnt.x < minX)
minX = pnt.x;

if (pnt.y > maxY)
maxY = pnt.y;

if (pnt.y < minY)
minY = pnt.y;




So then you can crop anywhere of the image once you have these points.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Bahramdun AdilBahramdun Adil

3,54541848




3,54541848












  • This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

    – HansHirse
    yesterday











  • @HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

    – Bahramdun Adil
    yesterday












  • @KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

    – Bahramdun Adil
    yesterday











  • ok sure, I will try this. right now I am just writing the code by looking at your code.

    – Karan Kanwal
    yesterday











  • @KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

    – Bahramdun Adil
    yesterday

















  • This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

    – HansHirse
    yesterday











  • @HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

    – Bahramdun Adil
    yesterday












  • @KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

    – Bahramdun Adil
    yesterday











  • ok sure, I will try this. right now I am just writing the code by looking at your code.

    – Karan Kanwal
    yesterday











  • @KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

    – Bahramdun Adil
    yesterday
















This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

– HansHirse
yesterday





This is (partly), what I suggested. Nevertheless, since you're only searching for maxY, the output of your solution for images, where the interesting part is at the bottom, won't be as desired by the question author as you always start at (0, 0).

– HansHirse
yesterday













@HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

– Bahramdun Adil
yesterday






@HansHirse Answer updated. I have already done the most important part of the algorithm, the rest you can do by yourself.

– Bahramdun Adil
yesterday














@KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

– Bahramdun Adil
yesterday





@KaranKanwal Most part of the code is very clear, it just uses the OpenCV API. cv:: is the namespace in C++, in Python, you can use cv. instead. And double is data type which in Python you no need to declare the type. I cannot write code in Python, but you can just consider the steps I have implemented the Algorithm, and convert it to Python Or you can let some of your friends about the C++ code to convert it to Python. This script already does what you want in your question.

– Bahramdun Adil
yesterday













ok sure, I will try this. right now I am just writing the code by looking at your code.

– Karan Kanwal
yesterday





ok sure, I will try this. right now I am just writing the code by looking at your code.

– Karan Kanwal
yesterday













@KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

– Bahramdun Adil
yesterday





@KaranKanwal If you are familiar with OpenCV API, then you can easily guess what I wrote in C++, at the most part it does not belong to the language used but belongs to the steps implemented. So do not be confused with C++ code above. Good Luck!

– Bahramdun Adil
yesterday












Karan Kanwal is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Karan Kanwal is a new contributor. Be nice, and check out our Code of Conduct.












Karan Kanwal is a new contributor. Be nice, and check out our Code of Conduct.











Karan Kanwal is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • 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.

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%2fstackoverflow.com%2fquestions%2f55023295%2fhow-to-crop-the-blank-part-from-an-image-document-form-in-python%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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme