We present a lattice-QCD determination of the elastic isospin-$1/2$ $S$-wave and $P$-wave $Kpi$ scattering amplitudes as a function of the center-of-mass energy using Luschers method. We perform global fits of $K$-matrix parametrizations to the finite-volume energy spectra for all irreducible representations with total momenta up to $sqrt{3}frac{2pi}{L}$; this includes irreps that mix the $S$- and $P$-waves. Several different parametrizations for the energy dependence of the $K$-matrix are considered. We also determine the positions of the nearest poles in the scattering amplitudes, which correspond to the broad $kappa$ resonance in the $S$-wave and the narrow $K^*(892)$ resonance in the $P$-wave. Our calculations are performed with $2+1$ dynamical clover fermions for two different pion masses of $317.2(2.2)$ and $175.9(1.8)$ MeV. Our preferred $S$-wave parametrization is based on a conformal map and includes an Adler zero; for the $P$-wave we use a standard pole parametrization including Blatt-Weisskopf barrier factors. The $S$-wave $kappa$-resonance pole positions are found to be $left[0.86(12) - 0.309(50),iright]:{rm GeV}$ at the heavier pion mass and $left[0.499(55)- 0.379(66),iright]:{rm GeV}$ at the lighter pion mass. The $P$-wave $K^*$-resonance pole positions are found to be $left[ 0.8951(64) - 0.00250(21),i right]:{rm GeV}$ at the heavier pion mass and $left[0.8718(82) - 0.0130(11),iright]:{rm GeV}$ at the lighter pion mass, which corresponds to couplings of $g_{K^* Kpi}=5.02(26)$ and $g_{K^* Kpi}=4.99(22)$, respectively.