Autocrop Checkpoint with fixed rotation.

Need to make sure that houghlinepcrop still works
correctly after the adjustments.

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2023-11-26 17:40:32 -05:00
parent 6465a49cfe
commit b149e86963
2 changed files with 111 additions and 48 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
@ -15,7 +15,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
@ -91,18 +91,18 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
"img = cv2.imread('/mnt/dataset/baseimages/15.jpg')\n",
"img = cv2.imread('/mnt/dataset/baseimages/6.jpg')\n",
"# img = cv2.imread('/mnt/code/autocropper/test_images/IMG_7605.jpg')\n",
"testall = False"
]
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
@ -448,7 +448,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
@ -468,11 +468,55 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"def houghlinedeskewthencrop(baseimage, preppedimage, rotationangle, croprect):\n",
" imgcopy = baseimage.copy()\n",
" # sizemultiplier = croprect[3]/preppedimage.shape[0]\n",
" \n",
" ##adjust the rotation angle if it causes the rectangle to flip w and h size ordering. That is, if it will cause the width to be greater than the height or something. let me think about it for a second more.\n",
" # print(baseimage.shape[:2])\n",
" mask = np.full(baseimage.shape[:2], fill_value=255, dtype=np.uint8)\n",
" mask = cv2.rectangle(mask, (croprect[0], croprect[1]), (croprect[0]+croprect[2], croprect[1]+croprect[3]), color=0, thickness=cv2.FILLED)\n",
" \n",
" \n",
" \n",
" # rotatedmask = mf.rotate(mask, rotationangle, fill=255)\n",
" # print(mask.shape)\n",
" # return mask, 5\n",
" rotatedmask = mf.rotatewithexactpadding(mask, rotationangle, fill=255)\n",
" rotatedbaseimage = mf.rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))\n",
" # return preppedimage, 5\n",
" rotateddst1 = mf.rotatewithexactpadding(preppedimage, rotationangle, fill=(0,0,0))\n",
" fcr = mf.croptoblack(rotatedmask, extraborder=0, returnrect=True) #finalcroprect\n",
" # return rotatedmask, 5\n",
" # return rotatedbaseimage, 5\n",
" # print(fcr)\n",
" rotatedbaseimage = rotatedbaseimage[fcr[1]:fcr[1]+fcr[3], fcr[0]:fcr[0]+fcr[2]]\n",
" # rotateddst1 = rotateddst1[fcr[1]:fcr[1]+fcr[3], fcr[0]:fcr[0]+fcr[2]]\n",
" # return mask, 5\n",
" # return rotatedbaseimage, 5\n",
" \n",
" \n",
" sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]\n",
" print(sizemultiplier)\n",
" # return rotatedbaseimage, rotationangle\n",
"\n",
" croppedbaseimage = mf.houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier)\n",
" \n",
" return croppedbaseimage, rotationangle\n",
"\n",
" # finalbaseimage = mf.contourcrop(croppedbaseimage)\n",
"\n",
"\n",
" return finalbaseimage, rotationangle\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" # rotateddst1 = rotatewithexactpadding(preppedimage, rotationangle, fill=0)\n",
" # rotatedbaseimage = rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))\n",
" # print(croprect)\n",
@ -481,31 +525,43 @@
" # print(hpad, vpad)\n",
" adjustedrect = [croprect[0]-hpad, croprect[1]-vpad, croprect[2]+(2*hpad), croprect[3]+(2*vpad)]\n",
" adjustedrect = [int(s) for s in adjustedrect]\n",
" leftpad = -min(0, adjustedrect[0])\n",
" toppad = -min(0, adjustedrect[1])\n",
" rightpad = -min(0, baseimage.shape[1]-(adjustedrect[0]+adjustedrect[2]))\n",
" bottompad = -min(0, baseimage.shape[0]-(adjustedrect[1]+adjustedrect[3]))\n",
" \n",
" adjustedrect = [max(0, s) for s in adjustedrect]\n",
"\n",
" # print(adjustedrect)\n",
" leftpad = abs(min(0, adjustedrect[0]))\n",
" toppad = abs(min(0, adjustedrect[1]))\n",
" rightpad = abs(min(0, baseimage.shape[1]-(adjustedrect[0]+adjustedrect[2])))\n",
" bottompad = abs(min(0, baseimage.shape[0]-(adjustedrect[1]+adjustedrect[3])))\n",
" \n",
" # print(leftpad, rightpad, toppad, bottompad)\n",
" \n",
" borderType = cv2.BORDER_CONSTANT\n",
" # padded = cv2.copyMakeBorder(baseimage, toppad, bottompad, leftpad, rightpad, borderType)\n",
" # imgcopy = padded.copy()\n",
" imgcopy = cv2.rectangle(imgcopy, (croprect[0], croprect[1]), (croprect[0]+croprect[2], croprect[1]+croprect[3]), color=(0,255,0), thickness=3)\n",
" imgcopy = cv2.rectangle(imgcopy, (adjustedrect[0], adjustedrect[1]), (adjustedrect[0]+adjustedrect[2], adjustedrect[1]+adjustedrect[3]), color=(0,0,255), thickness=3)\n",
" \n",
" adjustedrect = [max(0, s) for s in adjustedrect]\n",
" imgcopy = cv2.rectangle(imgcopy, (adjustedrect[0], adjustedrect[1]), (adjustedrect[0]+adjustedrect[2], adjustedrect[1]+adjustedrect[3]), color=(255,0,0), thickness=3)\n",
" return imgcopy, 5\n",
" \n",
"\n",
" ## DETERMINE PADDING AMOUNTS BECAUSE I THEN NEED TO PAD THE ORIGINAL IMAGE, CROP IT TO THE FIRST LEVEL, ROTATE, AND THEN CROP AGAIN\n",
" # print(adjustedrect)\n",
" # adjustedrect = croprect\n",
" borderType = cv2.BORDER_CONSTANT\n",
" padded = cv2.copyMakeBorder(baseimage, toppad, bottompad, leftpad, rightpad, borderType)\n",
" # borderType = cv2.BORDER_CONSTANT\n",
" # padded = cv2.copyMakeBorder(baseimage, toppad, bottompad, leftpad, rightpad, borderType)\n",
" croppedpaddedimage = padded[adjustedrect[1]:adjustedrect[1]+adjustedrect[3], adjustedrect[0]:adjustedrect[0]+adjustedrect[2], :]\n",
" # print(croprect[2], croprect[3])\n",
" # print(croppedpaddedimage.shape)\n",
" # croppedog = baseimage[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]\n",
" # return croppedpaddedimage, croppedog\n",
" # return croppedpaddedimage, 5\n",
" \n",
" rotatedbaseimage = mf.rotate(croppedpaddedimage, rotationangle)\n",
" \n",
" # print(vpad, hpad)\n",
" # print(croprect)\n",
" rotatedbaseimage = rotatedbaseimage[vpad:vpad+croprect[1]+croprect[3], hpad:hpad+croprect[0]+croprect[2], :]\n",
" # return rotatedbaseimage, 5\n",
" # rotatedbaseimage = rotatedbaseimage[vpad:vpad+croprect[3], hpad:hpad+croprect[2], :]\n",
" return rotatedbaseimage, 5\n",
" \n",
" \n",
" # print(preppedimage.shape)\n",
@ -529,7 +585,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
@ -555,7 +611,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
@ -586,7 +642,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
@ -595,9 +651,17 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 134,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.695\n"
]
}
],
"source": [
"# prepped, scaler, hp, vp = mf.squareandthenresize(img, fill=255, width=1000, returnscalerinfo=True)\n",
"outs = houghlineprocessing(img)\n",
@ -610,7 +674,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
@ -624,7 +688,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
@ -636,16 +700,16 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"testall = True"
"# testall = True"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
@ -655,7 +719,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
@ -685,17 +749,9 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 140,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"average time: 0.1456429362297058(s)\n"
]
}
],
"outputs": [],
"source": [
"if testall:\n",
" results = testondataset(\"/mnt/dataset/baseimages/\", houghlineprocessing)"
@ -703,7 +759,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
@ -713,7 +769,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
@ -722,7 +778,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [

View File

@ -400,6 +400,8 @@ def houghlinedeskewangle(image):
def determineextrapadding(h,w, angle):
radangle = abs(np.deg2rad(angle))
# print(type(h), type(w), type(angle))
# print(h, w, angle)
# print(radangle)
totalheightrot = w*np.sin(radangle) + h*np.cos(radangle)
# print(h, totalheightrot)
@ -411,10 +413,13 @@ def determineextrapadding(h,w, angle):
return hpad, vpad
def rotatewithexactpadding(img, angle, fill=(0,0,0)):
h, w = img[0], img[1]
hpad, vpad = determineextrapadding(w,h, angle)
h, w = img.shape[0], img.shape[1]
hpad, vpad = determineextrapadding(h=h,w=w, angle=angle)
# fill1 = fill
# print(fill)
baseimage = padWithColour(img, hpad, vpad, fill=fill)
rotatedimg = rotate(baseimage, angle)
# return baseimage
rotatedimg = rotate(baseimage, angle,fill=fill)
return rotatedimg
def houghlinepcrop(baseimage, preppedimage, scalingmultiplier):
@ -727,7 +732,7 @@ def cropclarifying(image):
return lineout
# implement a function that's called refine text
def croptoblack(image, extraborder=10):
def croptoblack(image, extraborder=10, returnrect=False):
invertedimage = cv2.bitwise_not(image)
blackpixels = cv2.findNonZero(invertedimage)
mins = np.min(blackpixels, axis=0)
@ -737,6 +742,8 @@ def croptoblack(image, extraborder=10):
maxx = min(maxs[0][0]+extraborder, image.shape[1])
maxy = min(maxs[0][1]+extraborder, image.shape[0])
# print(blackpixels)
if (returnrect):
return [minx,miny,maxx-minx,maxy-miny]
return image[miny:maxy, minx:maxx]
def reduceColours(x, centering=127):