Class OpenCVService
getInstance()
method.
To perform image processing operations, use the methods provided by this class.
This class requires the OpenCV library to be loaded locally using OpenCV.loadLocally()
.
It also uses environment variables to configure certain parameters.
The required environment variables are:
- MIN_CARD_AREA: Minimum area of a card contour (default: 20000)
- MAX_CARD_AREA: Maximum area of a card contour (default: 30000)
- MAX_BLACK_PERCENTAGE: Maximum percentage of black pixels in a card corner (default: 90)
You can refer to the CardAreaTest class for consulting the minimum and maximum card area values.
This class logs important information using Log4j2.
The main operations provided by this class are:
- Treating an image by blurring and applying Canny edge detection (treatImage(Mat)
).
- Finding ordered and external contours in an image (returnOrderedAndExternalContours(Mat)
).
- Extracting all card contours from an image (getAllCardsContours(Mat)
).
- Getting the dimensions of a card contour (getCardDimensions(CardContour)
).
- Cutting and flattening a card image (cutAndFlatCard(Mat, CardDimensions, boolean)
).
- Extracting the corner of a card image (getCorner(Mat)
).
- Predicting the rank and suit of a card based on its corner image (predictCard(CardCorner)
).
- Processing a card by extracting its information (getProcessedCard(CardContour, CardDimensions)
).
- Drawing text, contours, and cards on an image (drawText(Mat, String, int)
, drawContours(Mat, List)
, drawCards(Mat, List)
).
- Author:
- Igor Oliveira
-
Method Summary
Modifier and TypeMethodDescriptionorg.opencv.core.Mat
cutAndFlatCard
(org.opencv.core.Mat originalFrame, CardDimensions cardDimensions, boolean invert) Cuts and flattens a card from the original frame based on the card dimensions.void
Draws the cards on the input frame.void
drawContours
(org.opencv.core.Mat frame, List<CardContour> cardContours) Draws the card contours on the input frame.void
Draws text on the input frame.getAllCardsContours
(org.opencv.core.Mat frame) Extracts all card contours from the input frame.getCardDimensions
(CardContour contour) Calculates the dimensions of a card contour.getCorner
(org.opencv.core.Mat flattenedCardImage) Extracts the corner of a flattened card image.static OpenCVService
Returns the singleton instance of the OpenCVService class.getProcessedCard
(CardContour cardContour, CardDimensions cardDimensions) Processes a card by extracting its information.predictCard
(CardCorner corner) Predicts the rank and suit of a card based on its corner images.List<org.opencv.core.MatOfPoint>
returnOrderedAndExternalContours
(org.opencv.core.Mat frame) Finds the ordered and external contours in the input frame.org.opencv.core.Mat
treatImage
(org.opencv.core.Mat frame) Applies image processing operations to the input frame.
-
Method Details
-
getInstance
Returns the singleton instance of the OpenCVService class. If the instance doesn't exist, it creates a new one.- Returns:
- The OpenCVService instance.
-
treatImage
public org.opencv.core.Mat treatImage(org.opencv.core.Mat frame) Applies image processing operations to the input frame. It blurs the frame using a 2x2 kernel and applies Canny edge detection.- Parameters:
frame
- The input frame to be processed.- Returns:
- The processed frame with highlighted edges.
-
returnOrderedAndExternalContours
Finds the ordered and external contours in the input frame. It uses thetreatImage(Mat)
method to preprocess the frame and then finds the contours using the findContours method. The contours are ordered based on their area, and only the external contours are returned.- Parameters:
frame
- The input frame to find contours in.- Returns:
- The list of external contours ordered by area.
-
getAllCardsContours
Extracts all card contours from the input frame. It uses thereturnOrderedAndExternalContours(Mat)
method to get the ordered and external contours, and then filters the contours based on their area and shape to extract the card contours.- Parameters:
frame
- The input frame to extract card contours from.- Returns:
- The list of card contours.
-
getCardDimensions
Calculates the dimensions of a card contour. It calculates the center coordinates, width, and height of the card contour's bounding rectangle.- Parameters:
contour
- The card contour to calculate dimensions for.- Returns:
- The dimensions of the card contour.
-
cutAndFlatCard
public org.opencv.core.Mat cutAndFlatCard(org.opencv.core.Mat originalFrame, CardDimensions cardDimensions, boolean invert) Cuts and flattens a card from the original frame based on the card dimensions. It uses perspective transformation to cut and flatten the card image. The output image is resized to a fixed width and height.- Parameters:
originalFrame
- The original frame containing the card.cardDimensions
- The dimensions of the card contour.invert
- Indicates whether to invert the card image.- Returns:
- The cut and flattened card image.
-
getCorner
Extracts the corner of a flattened card image. It takes a submat of the card image to extract the corner region. The corner region is then resized and thresholded to get the corner rank and suit images.- Parameters:
flattenedCardImage
- The flattened card image.- Returns:
- The corner information including the full corner image, corner rank image, and corner suit image.
-
predictCard
Predicts the rank and suit of a card based on its corner images. It performs template matching to find the best match for the corner rank and suit images. The rank and suit with the highest match score are selected as the predicted values.- Parameters:
corner
- The corner information including the corner rank and suit images.- Returns:
- The predicted rank and suit of the card.
-
getProcessedCard
Processes a card by extracting its information. It cuts and flattens the card image, extracts the corner information, and predicts the rank and suit of the card. If the percentage of black pixels in the corner exceeds the maximum threshold, it cuts and flattens the card image again with an inverted perspective transformation to improve prediction accuracy.- Parameters:
cardContour
- The card contour information.cardDimensions
- The dimensions of the card contour.- Returns:
- The processed card object containing the card image, contour information, dimensions, and predicted rank and suit.
-
drawText
Draws text on the input frame.- Parameters:
frame
- The input frame to draw text on.text
- The text to be drawn.line
- The line number to position the text vertically.
-
drawContours
Draws the card contours on the input frame.- Parameters:
frame
- The input frame to draw the card contours on.cardContours
- The list of card contours.
-
drawCards
Draws the cards on the input frame. It draws the card contours and predicts the rank and suit for each card, and draws the corresponding text on the frame.- Parameters:
frame
- The input frame to draw the cards on.cards
- The list of cards.
-