Ask Your Question
2

How to split image into small blocks, process on them and then join all the blocks together again?

asked 2017-09-08 06:16:55 -0600

Santhosh1 gravatar image

updated 2020-11-16 16:34:51 -0600

image description

I want to split an image into block and perform some operation on each block separately. Then join the blocks again into a single image(same shape as the original before splitting).

I am writing code in python not C++.

Any suggestions on how to do this?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-09-08 07:45:49 -0600

updated 2017-09-08 08:27:28 -0600

try to convert the code in this post or the code below

Mat src = imread("e:/getty-apple_large.jpg");

int width = src.cols;
int height = src.rows;
int GRID_SIZE = 100;

vector<Rect> mCells;

for (int y = 0; y < height - GRID_SIZE; y += GRID_SIZE) {
    for (int x = 0; x < width - GRID_SIZE; x += GRID_SIZE) {
        int k = x*y + x;
        Rect grid_rect(x, y, GRID_SIZE, GRID_SIZE);
        cout << grid_rect<< endl;
        mCells.push_back(grid_rect);
        rectangle(src, grid_rect, Scalar(0, 255, 0), 1);
        imshow("src", src);
        imshow(format("grid%d",k), src(grid_rect));
        waitKey();
    }
}
edit flag offensive delete link more

Comments

Yup I did a similar thing on python using WHILE loop ๐Ÿ‘

Santhosh1 gravatar imageSanthosh1 ( 2017-09-13 06:06:52 -0600 )edit

you can post your own answer and share your code here maybe it will be helpful for someone.

sturkmen gravatar imagesturkmen ( 2017-09-13 06:12:52 -0600 )edit

can you post your code? it will helpful for me.

HINAL03 gravatar imageHINAL03 ( 2019-02-07 03:30:42 -0600 )edit
0

answered 2020-02-26 15:51:47 -0600

# Image path, number of rows 
# and number of columns 
# should be provided as an arguments
import cv2
import sys
import os


if not os.path.exists('patches'):
    os.makedirs('patches')



nRows = int(sys.argv[2])
# Number of columns
mCols = int(sys.argv[3])

# Reading image
img = cv2.imread(sys.argv[1])
#print img

#cv2.imshow('image',img)

# Dimensions of the image
sizeX = img.shape[1]
sizeY = img.shape[0]

print(img.shape)


for i in range(0,nRows):
    for j in range(0, mCols):
        roi = img[i*sizeY/nRows:i*sizeY/nRows + sizeY/nRows ,j*sizeX/mCols:j*sizeX/mCols + sizeX/mCols]
        cv2.imshow('rois'+str(i)+str(j), roi)
                cv2.imwrite('patches/patch_'+str(i)+str(j)+".jpg", roi)



cv2.waitKey()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-08 06:16:55 -0600

Seen: 33,122 times

Last updated: Sep 08 '17