You should the logic of taking the max area of rectangles as shown below. You can use this snippet to get the mask image of the area:

import cv2
import numpy as np

image = cv2.imread("jigsaw.jpg")

image = cv2.resize(image, (image.shape[1] // 4, image.shape[0] // 4))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)

thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

max_area = 0
best_cnt = None
for counter in contours:
    area = cv2.contourArea(counter)
    if area > 1000:
        if area > max_area:
            max_area = area
            best_cnt = counter

mask = np.zeros((gray.shape), np.uint8)

cv2.drawContours(mask, [best_cnt], 0, 255, -1)
cv2.drawContours(mask, [best_cnt], 0, 0, 2)

cv2.imwrite('mask_jigsaw.jpg', mask)

cv2.imshow("Image Mask", mask)
cv2.waitKey(0)

Input image:

Output image:

Answer from yakhyo on Stack Overflow
🌐
PyImageSearch
pyimagesearch.com › home › blog › image masking with opencv
Image Masking with OpenCV - PyImageSearch
November 10, 2024 - OpenCV Image Masking is powerful for manipulating images. It allows you to apply effects to a single image and create an entirely new look.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 96817 › how-to-create-mask-from-an-image-in-opencv
How to create mask from an image in opencv? - OpenCV Q&A Forum
June 19, 2016 - there is an offical sample create_mask.cpp for doing that. or you can try the following code that implemented before. #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; using namespace std; Mat src,cloneimg; bool mousedown; vector<vector<Point> > contours; vector<Point> pts; void onMouse( int event, int x, int y, int flags, void* userdata ) { Mat img = *((Mat *)userdata); if( event == EVENT_LBUTTONDOWN ) { mousedown = true; contours.clear(); pts.clear(); } if( event == EVENT_LBUTTONUP ) { mousedown = false; if(pts.size() > 2 ) { Mat mask(img.size(),CV_8UC1); mask
🌐
OpenCV
docs.opencv.org › 4.13.0 › db › d75 › samples_2cpp_2create_mask_8cpp-example.html
OpenCV: samples/cpp/create_mask.cpp
This program demonstrates using mouse events and how to make and use a mask image (black and white) . ... Designed for command line parsing. ... Creates a full copy of the array and the underlying data.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 228538 › how-to-create-a-binary-mask-for-medical-images
How to create a Binary Mask for medical images - OpenCV Q&A Forum
April 6, 2020 - Hello, I'm try to create binary mask for medical images, im new to OpenCV and any advice will help on how, or if it possible, to do so with OpenCV. Below is the type of pictures I try to create mask for: I need to color the vocal cords area in white(Marked with triangle) and the rest in black, so it will look something like that: i tried to find the contours using that code: import cv2 import numpy as np img = cv2.imread("LPR.png", cv2.IMREAD_GRAYSCALE) _, threshold = cv2.threshold(img, 50, 255, cv2.
🌐
DigitalOcean
digitalocean.com › community › tutorials › arithmetic-bitwise-and-masking-python-opencv
Image Processing Part 5: Arithmetic, Bitwise, and Masking | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
YouTube
youtube.com › watch
Learn How to apply image masking in opencv in 8 mins | Opencv Tutorial - YouTube
Welcome to DWBIADDA's computer vision ( OpenCV ) tutorial, as part of this lecture we are going to learn,Learn How to apply image masking in opencv
Published: January 18, 2021
🌐
OpenCV
docs.opencv.org › 4.13.0 › d7 › d37 › tutorial_mat_mask_operations.html
OpenCV: Mask operations on matrices
You can download this source code from here or look in the OpenCV source code libraries sample directory at samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp. ... Template matrix class derived from Mat. ... Sets all or some of the array elements to the specified value. ... Creates ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-mask-an-image-in-opencv-python
How to mask an image in OpenCV Python?
August 25, 2023 - We can apply a mask to an image by computing the cv2.bitwise_and() between the mask and the image. To track a color, we define a mask in HSV color space using passing lower and upper limits of color values in HSV.
Find elsewhere
🌐
YouTube
youtube.com › data magic (by sunny kusawa)
Image Masking using OpenCV l Mask Image | Computer Vision | Data Magic - YouTube
Hello Friends, Here is an new computer vision episode on How to mask image.We are going to conver RGB color image into HSV image and after that will create a...
Published: November 6, 2020
Views: 22K
🌐
Medium
medium.com › @sasaniperera › opencv-object-masking-b3143e310d03
OpenCV: Object Masking. Object masking is very important in… | by Sasani Perera | Medium
August 14, 2023 - We need to change the range of Saturation and Value until we get a satisfactory mask. ... Almost there! Let’s fine-tune it more. ... Looks good!! So this is how we extract an object of a certain colour (range) from an image. ... #import necessary libraries import cv2 import numpy as np import matplotlib.pyplot as plt #using opencv to read an image #BGR Image plate_image_1 = cv2.imread("C:/users/public/pictures/numberp_1.png") #converting BGR to HSV HSV_im_1 = cv2.cvtColor(plate_image_1,cv2.COLOR_BGR2HSV) #defining HSV range Orange_UB = np.array([40,255,245]) Orange_LB = np.array([20,55,180]) #masking mask = cv2.inRange(HSV_im_1,Orange_LB,Orange_UB) #visualizing cv2.namedWindow("HSV Orange masked", cv2.WINDOW_NORMAL); cv2.imshow("HSV Orange masked",mask); cv2.waitKey(0) & 0xFF cv2.destroyAllWindows()
🌐
Medium
medium.com › analytics-vidhya › masking-an-area-in-a-video-in-opencv-in-python-harry-potter-invisible-cloak-example-a25279fdc26c
Masking an area in a video in OpenCV in Python— Harry Potter Invisible Cloak example | by Venkatesh Chandra | Analytics Vidhya | Medium
February 20, 2020 - The code in this block creates mask objects with coordinates to detect the color specified. Colors are specified in the HSV format in the range. In this example, we are specifying light blue color.
🌐
GitHub
github.com › opencv › opencv › blob › master › samples › cpp › create_mask.cpp
opencv/samples/cpp/create_mask.cpp at master · opencv/opencv
* create_mask.cpp · * * Author: * Siddharth Kherada <siddharthkherada27[at]gmail[dot]com> * * This tutorial demonstrates how to make mask image (black and white). * The program takes as input a source image and outputs its corresponding · * mask image. */ · #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include <iostream> ·
Author: opencv
🌐
FreedomVC
freedomvc.com › home › image › opencv › image thresholding
Image Thresholding and Masks with OpenCV - FreedomVC
June 6, 2021 - Image thresholding allows us to create an image mask by applying a “threshold” to determine whether each pixel is of interest to us or not.
🌐
GeeksforGeeks
geeksforgeeks.org › python › masking-in-python-opencv
Masking in Python OpenCV - GeeksforGeeks
February 13, 2026 - OpenCV performs masking using cv2.inRange() which selects pixels within a defined color range. To improve accuracy, frames are often smoothed with cv2.blur() before masking to reduce noise.
🌐
YouTube
youtube.com › onetouchbi
OpenCV Python Tutorial - Create Foreground Mask for the Moving Objects | Part 1 | OneTouchBI - YouTube
Welcome to the knowledge base portal - WWW.ONETOUCHBI.COM. It's the platform to connect all BI Associates under one umbrella.OpenCV Python Tutorial - Create ...
Published: November 22, 2020
Views: 4K
🌐
OpenCV
forum.opencv.org › uncategorized
OpenCV: Create mask for certificate - Uncategorized - OpenCV
May 26, 2021 - I'm currently working on my first project in regards to Image Processing and OpenCV. I'm using [OpenCvSharp][1] as an wrapper for C#, but I'm able to translate code from any other programming language like Python or C++. Description For my project, I receive images (photos or scans) of certificates, my job now is to mask, cut out and then transform them.
🌐
YouTube
youtube.com › watch
Contours & Masks using Python & OpenCV
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.