Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +132 -1
pages/Data Collection.py
CHANGED
|
@@ -1396,6 +1396,9 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1396 |
|
| 1397 |
# Code Example
|
| 1398 |
st.code("""
|
|
|
|
|
|
|
|
|
|
| 1399 |
# Define the rotation matrix
|
| 1400 |
r_m = cv2.getRotationMatrix2D((1347, 900), 50, 1) # Center at (1347, 900), Rotate by 50 degrees, Scale = 1
|
| 1401 |
|
|
@@ -1410,11 +1413,15 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1410 |
cv2.destroyAllWindows()
|
| 1411 |
""", language="python")
|
| 1412 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1413 |
st.write("""
|
| 1414 |
OpenCV provides a direct method for rotating images with predefined angles: `cv2.rotate`.
|
| 1415 |
This method simplifies rotation operations for 90°, 180°, and 270° (clockwise or counterclockwise) without requiring a custom rotation matrix.
|
| 1416 |
|
| 1417 |
-
### Available Rotation Modes:
|
| 1418 |
- **`cv2.ROTATE_180`**: Rotates the image by 180°.
|
| 1419 |
- **`cv2.ROTATE_90_CLOCKWISE`**: Rotates the image by 90° clockwise.
|
| 1420 |
- **`cv2.ROTATE_90_COUNTERCLOCKWISE`**: Rotates the image by 90° counterclockwise.
|
|
@@ -1437,10 +1444,134 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1437 |
cv2.destroyAllWindows()
|
| 1438 |
""", language="python")
|
| 1439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1440 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1441 |
|
| 1442 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
|
|
|
| 1396 |
|
| 1397 |
# Code Example
|
| 1398 |
st.code("""
|
| 1399 |
+
# Load the image
|
| 1400 |
+
img = cv2.imread('path_to_image.jpg')
|
| 1401 |
+
|
| 1402 |
# Define the rotation matrix
|
| 1403 |
r_m = cv2.getRotationMatrix2D((1347, 900), 50, 1) # Center at (1347, 900), Rotate by 50 degrees, Scale = 1
|
| 1404 |
|
|
|
|
| 1413 |
cv2.destroyAllWindows()
|
| 1414 |
""", language="python")
|
| 1415 |
|
| 1416 |
+
# Explanation for Direct Rotation
|
| 1417 |
+
st.markdown("""
|
| 1418 |
+
<h3 style="color: #9400d3;">Direct Rotation Using cv2.rotate</h3>
|
| 1419 |
+
""", unsafe_allow_html=True)
|
| 1420 |
+
|
| 1421 |
st.write("""
|
| 1422 |
OpenCV provides a direct method for rotating images with predefined angles: `cv2.rotate`.
|
| 1423 |
This method simplifies rotation operations for 90°, 180°, and 270° (clockwise or counterclockwise) without requiring a custom rotation matrix.
|
| 1424 |
|
|
|
|
| 1425 |
- **`cv2.ROTATE_180`**: Rotates the image by 180°.
|
| 1426 |
- **`cv2.ROTATE_90_CLOCKWISE`**: Rotates the image by 90° clockwise.
|
| 1427 |
- **`cv2.ROTATE_90_COUNTERCLOCKWISE`**: Rotates the image by 90° counterclockwise.
|
|
|
|
| 1444 |
cv2.destroyAllWindows()
|
| 1445 |
""", language="python")
|
| 1446 |
|
| 1447 |
+
# Explanation for Shearing
|
| 1448 |
+
st.markdown("""
|
| 1449 |
+
<h3 style="color: #9400d3;">Shearing</h3>
|
| 1450 |
+
""", unsafe_allow_html=True)
|
| 1451 |
+
|
| 1452 |
+
st.write("""
|
| 1453 |
+
**Shearing** is a transformation that slants the shape of an image along the x-axis, y-axis, or both. It skews the content of the image, creating a shifted or stretched effect.
|
| 1454 |
+
|
| 1455 |
+
The transformation is performed using a shearing matrix:
|
| 1456 |
+
""")
|
| 1457 |
+
|
| 1458 |
+
st.write("""
|
| 1459 |
+
The shearing matrix is represented as:
|
| 1460 |
+
|
| 1461 |
+
For x-axis shear:
|
| 1462 |
+
[[1, shx, 0], [0, 1, 0]]
|
| 1463 |
+
|
| 1464 |
+
For y-axis shear:
|
| 1465 |
+
[[1, 0, 0], [shy, 1, 0]]
|
| 1466 |
+
|
| 1467 |
+
Here:
|
| 1468 |
+
- **shx**: Shear factor along the x-axis.
|
| 1469 |
+
- **shy**: Shear factor along the y-axis.
|
| 1470 |
+
""")
|
| 1471 |
|
| 1472 |
+
st.code("""
|
| 1473 |
+
# Load the image
|
| 1474 |
+
img = cv2.imread('path_to_image.jpg')
|
| 1475 |
+
|
| 1476 |
+
# Define shearing parameters
|
| 1477 |
+
shx = 1 # Shear factor along the x-axis
|
| 1478 |
+
shy = 3 # Shear factor along the y-axis
|
| 1479 |
+
tx = 0 # Translation along the x-axis
|
| 1480 |
+
ty = 0 # Translation along the y-axis
|
| 1481 |
+
|
| 1482 |
+
# Create the shearing matrix
|
| 1483 |
+
shearing_matrix = np.array([[1, shx, tx], [shy, 1, ty]], dtype=np.float32)
|
| 1484 |
+
|
| 1485 |
+
# Apply the shearing transformation
|
| 1486 |
+
sheared_img = cv2.warpAffine(img, shearing_matrix, (300, 300))
|
| 1487 |
+
|
| 1488 |
+
# Display the original and sheared images
|
| 1489 |
+
cv2.imshow("Original Image", img)
|
| 1490 |
+
cv2.imshow("Sheared Image", sheared_img)
|
| 1491 |
+
|
| 1492 |
+
cv2.waitKey(0)
|
| 1493 |
+
cv2.destroyAllWindows()
|
| 1494 |
+
""", language="python")
|
| 1495 |
|
| 1496 |
|
| 1497 |
+
# Explanation for Scaling
|
| 1498 |
+
st.markdown("""
|
| 1499 |
+
<h3 style="color: #9400d3;">Scaling</h3>
|
| 1500 |
+
""", unsafe_allow_html=True)
|
| 1501 |
+
|
| 1502 |
+
st.write("""
|
| 1503 |
+
**Scaling** is a transformation that changes the size of an image. It can be used to enlarge or shrink the image while maintaining its original proportions or altering them.
|
| 1504 |
+
|
| 1505 |
+
Scaling is performed using a scaling matrix:
|
| 1506 |
+
""")
|
| 1507 |
+
|
| 1508 |
+
st.write("""
|
| 1509 |
+
The scaling matrix is represented as:
|
| 1510 |
|
| 1511 |
+
[[sx, 0, 0], [0, sy, 0]]
|
| 1512 |
+
|
| 1513 |
+
Here:
|
| 1514 |
+
- **sx**: Scaling factor along the x-axis.
|
| 1515 |
+
- **sy**: Scaling factor along the y-axis.
|
| 1516 |
+
- If `sx` and `sy` are greater than 1, the image is enlarged.
|
| 1517 |
+
- If `sx` and `sy` are less than 1, the image is shrunk.
|
| 1518 |
+
""")
|
| 1519 |
+
|
| 1520 |
+
st.code("""
|
| 1521 |
+
# Load the image
|
| 1522 |
+
img = cv2.imread('path_to_image.jpg')
|
| 1523 |
+
|
| 1524 |
+
# Define scaling and translation parameters
|
| 1525 |
+
sx, sy = 2, 1 # Scale by 2 along the x-axis and 1 along the y-axis
|
| 1526 |
+
tx, ty = 0, 0 # No translation
|
| 1527 |
+
|
| 1528 |
+
# Create the scaling matrix
|
| 1529 |
+
scaling_matrix = np.array([[sx, 0, tx], [0, sy, ty]], dtype=np.float32)
|
| 1530 |
+
|
| 1531 |
+
# Apply scaling
|
| 1532 |
+
scaled_img = cv2.warpAffine(img, scaling_matrix, (2 * 300, 300))
|
| 1533 |
+
|
| 1534 |
+
# Display the images
|
| 1535 |
+
cv2.imshow("Original Image", img)
|
| 1536 |
+
cv2.imshow("Scaled Image", scaled_img)
|
| 1537 |
+
|
| 1538 |
+
cv2.waitKey(0)
|
| 1539 |
+
cv2.destroyAllWindows()
|
| 1540 |
+
""", language="python")
|
| 1541 |
+
|
| 1542 |
+
|
| 1543 |
+
# Explanation for Cropping
|
| 1544 |
+
st.markdown("""
|
| 1545 |
+
<h3 style="color: #9400d3;">Cropping</h3>
|
| 1546 |
+
""", unsafe_allow_html=True)
|
| 1547 |
+
|
| 1548 |
+
st.write("""
|
| 1549 |
+
**Cropping** is a transformation that extracts a specific portion of an image, usually to focus on a region of interest.
|
| 1550 |
+
It is achieved by selecting a rectangular region of the image using pixel coordinates.
|
| 1551 |
+
|
| 1552 |
+
The process involves defining the coordinates for:
|
| 1553 |
+
- **Top-left corner (x1, y1)**: Starting point of the crop.
|
| 1554 |
+
- **Bottom-right corner (x2, y2)**: Ending point of the crop.
|
| 1555 |
+
""")
|
| 1556 |
+
|
| 1557 |
+
st.code("""
|
| 1558 |
+
# Load the image
|
| 1559 |
+
img = cv2.imread('path_to_image.jpg')
|
| 1560 |
+
|
| 1561 |
+
# Define crop coordinates
|
| 1562 |
+
x1, y1 = 50, 50 # Top-left corner
|
| 1563 |
+
x2, y2 = 200, 200 # Bottom-right corner
|
| 1564 |
+
|
| 1565 |
+
# Crop the image
|
| 1566 |
+
cropped_img = img[y1:y2, x1:x2]
|
| 1567 |
+
|
| 1568 |
+
# Display the images
|
| 1569 |
+
cv2.imshow("Original Image", img)
|
| 1570 |
+
cv2.imshow("Cropped Image", cropped_img)
|
| 1571 |
+
|
| 1572 |
+
cv2.waitKey(0)
|
| 1573 |
+
cv2.destroyAllWindows()
|
| 1574 |
+
""", language="python")
|
| 1575 |
|
| 1576 |
|
| 1577 |
|